为什么 os.path.getsize() 在 python 中抛出错误?
Why os.path.getsize() throw error in python?
我正在尝试打印所有文件列表的大小,但出现错误。
这是代码:
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'
不知道怎么回事。我认为它与 'concealed files' 有点相关,但事实并非如此。
试试这个:
printSize(os.path.abspath(os.path.join(folderName, filename)))
来自 os.walk
的文档(强调已添加):
Generate the file names in a directory tree by walking the tree either top-down or bottom-up. For each directory in the tree rooted at directory top (including top itself), it yields a 3-tuple (dirpath, dirnames, filenames).
dirpath is a string, the path to the directory. dirnames is a list of the names of the subdirectories in dirpath (excluding '.' and '..'). filenames is a list of the names of the non-directory files in dirpath. Note that the names in the lists contain no path components. To get a full path (which begins with top) to a file or directory in dirpath, do os.path.join(dirpath, name).
我猜你 运行 你的脚本在 AutomateBoringStuff
中(所以它是工作目录),但文件实际上在 Desktop
.
中
/Users/kris/Desktop/AutomateBoringStuff/BLE Scanner.m4a
应该是 /Users/kris/Desktop/BLE Scanner.m4a
根据docs.python
os.path.getsize(路径)
Return 路径的大小(以字节为单位)。
Raise os.error if the file does not exist or is inaccessible
因此捕获此异常或确保文件存在。
我正在尝试打印所有文件列表的大小,但出现错误。 这是代码:
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'
不知道怎么回事。我认为它与 'concealed files' 有点相关,但事实并非如此。
试试这个:
printSize(os.path.abspath(os.path.join(folderName, filename)))
来自 os.walk
的文档(强调已添加):
Generate the file names in a directory tree by walking the tree either top-down or bottom-up. For each directory in the tree rooted at directory top (including top itself), it yields a 3-tuple (dirpath, dirnames, filenames). dirpath is a string, the path to the directory. dirnames is a list of the names of the subdirectories in dirpath (excluding '.' and '..'). filenames is a list of the names of the non-directory files in dirpath. Note that the names in the lists contain no path components. To get a full path (which begins with top) to a file or directory in dirpath, do os.path.join(dirpath, name).
我猜你 运行 你的脚本在 AutomateBoringStuff
中(所以它是工作目录),但文件实际上在 Desktop
.
/Users/kris/Desktop/AutomateBoringStuff/BLE Scanner.m4a
应该是 /Users/kris/Desktop/BLE Scanner.m4a
根据docs.python os.path.getsize(路径) Return 路径的大小(以字节为单位)。
Raise os.error if the file does not exist or is inaccessible
因此捕获此异常或确保文件存在。