【发布时间】:2014-12-29 19:20:57
【问题描述】:
我正在尝试计算我的文件夹中的文件数 (/Home/python) 为此我做了一个小程序
import os.path
path = '/Home/python'
num_files = len([f for f in os.listdir(path)if os.path.isfile(os.path.join(path, f))])
但它给了我这样的错误
Traceback (most recent call last):
File "count_number_of_files_folder.py", line 3, in <module>
num_files = len([f for f in os.listdir(path)if os.path.isfile(os.path.join(path, f))])
OSError: [Errno 2] No such file or directory: '/Home/python'
伙计们,你能帮我制作一个没有错误的程序吗? 谢谢你
【问题讨论】:
-
我认为它是绝对路径,但确切地说我不知道绝对路径的定义,但你可以在我的问题中查看 /Home/python .. 我刚刚添加了该图像
-
是
home小写h -
H 在大写 ome 中是小写
-
@PadraicCunningham:其实是 /home/username/python
-
@PadraicCunningham 我也尝试使用用户名,但仍然相同的错误 Traceback(最近一次调用最后一次):文件“count_number_of_files_folder2.py”,第 4 行,在
num_files = len([f for f in os.listdir(path)if os.path.isfile(os.path.join(path, f))]) OSError: [Errno 2] No such file or directory: '/Home/tansen/python'
标签: python file count numbers directory