我无法获得Python文件夹的大小

问题描述 投票:0回答:2

我知道有这样的问题,但是我仍然想问这个,因为我无法解决,所以有我的代码:

#! 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)而不是得到它们的大小时,它显示的是文件夹,所以我认为程序找不到它们。

python filenotfoundexception os.path
2个回答
0
投票

问题可能是您将os.path.getsize()作为参数传递的只是文件夹名称,而不是文件夹的整个路径


0
投票

可能是您的文件名为'FordvFerrari1080p.mp4',而不是'FordvFerrari1080p.mp4'(或任何文件类型)

© www.soinside.com 2019 - 2024. All rights reserved.