我试图打印所有文件列表的大小,但它抛出错误。 这是代码:
import os
def printSize(fileAbsPath) :
print os.path.getsize(fileAbsPath)
for folderName, subfolders, filenames in os.walk('/Users/kris/Desktop'):
for filename in filenames :
try :
printSize(os.path.abspath(filename))
except Exception as err:
print 'An exception happend : ' + str(err)
并且,错误是:
An exception happend : [Errno 2] No such file or directory: '/Users/kris/Desktop/AutomateBoringStuff/.DS_Store'
An exception happend : [Errno 2] No such file or directory: '/Users/kris/Desktop/AutomateBoringStuff/.gitignore'
An exception happend : [Errno 2] No such file or directory: '/Users/kris/Desktop/AutomateBoringStuff/.gitmodules'
An exception happend : [Errno 2] No such file or directory: '/Users/kris/Desktop/AutomateBoringStuff/.localized'
.
.
.
.
An exception happend : [Errno 2] No such file or directory: '/Users/kris/Desktop/AutomateBoringStuff/BLE Scanner.m4a'
An exception happend : [Errno 2] No such file or directory: '/Users/kris/Desktop/AutomateBoringStuff/Screen Shot 2015-07-23 at 3.30.52 PM.png'
我不知道这是怎么回事。我认为这与“隐藏文件”有点相关,但事实并非如此。
最佳答案
试试这个:
printSize(os.path.abspath(os.path.join(folderName, filename)))
关于python - 为什么 os.path.getsize() 在 python 中抛出错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33852057/