我知道有这样的问题,但是我仍然想问这个,因为我无法解决,所以有我的代码:
#! python3
import os
my_path = 'E:\\Movies'
for folder in os.listdir(my_path):
size = os.path.getsize(folder)
print(f'size of the {folder} is: {size} ')
我收到此错误:
Traceback (most recent call last):
File "c:/Users/ataba/OneDrive/Masaüstü/Programming/python/findingfiles.py", line 7, in <module>
size = os.path.getsize(folder)
File "C:\Program Files\Python37\lib\genericpath.py", line 50, in getsize
return os.stat(filename).st_size
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'FordvFerrari1080p'
当我写print(folder)
而不是得到它们的大小时,它显示的是文件夹,所以我认为程序找不到它们。
问题可能是您将os.path.getsize()
作为参数传递的只是文件夹名称,而不是文件夹的整个路径
可能是您的文件名为'FordvFerrari1080p.mp4',而不是'FordvFerrari1080p.mp4'(或任何文件类型)