Fah*_*lis 1 python directory numbers file count
我正在尝试计算我的文件夹(/ 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))])
Run Code Online (Sandbox Code Playgroud)
但它给了我一个这样的错误
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'
Run Code Online (Sandbox Code Playgroud)
你可以帮助我帮助我制作无bug程序.谢谢
小智 9
我认为这是最简单的方法:
import os
img_folder_path = 'C:/FolderName/FolderName2/IMG/'
dirListing = os.listdir(img_folder_path)
print(len(dirListing))
Run Code Online (Sandbox Code Playgroud)
试试这个
import os.path
path = os.getenv('HOME') + '/python'
num_files = len([f for f in os.listdir(path)if os.path.isfile(os.path.join(path, f))])
Run Code Online (Sandbox Code Playgroud)
这是因为你不在/ home/python中.你实际上在你的主目录是/ home/$ USER/python.如果您执行以下操作,在终端中,您将看到您所在的位置.
cd ~/python && pwd
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
16050 次 |
最近记录: |