【发布时间】:2012-05-15 21:13:36
【问题描述】:
我正在尝试获取获取目录大小的函数。
def fsize(path, returntype='b'):
size = 0
if isdir(path):
for root, dirs, files in walk(path):
for file in files:
size += getsize(join(path,file))
else:
print abspath(path)
size = getsize(abspath(path))
if returntype != 'b':
return convert_size(size, returntype)
return size
path = r"D:\Library\Daniel's_Books"
print fsize(path, 'm')
我得到了这个有趣的错误:
size = getsize(abspath(path))
File "C:\Python27\lib\genericpath.py", line 49, in getsize
return os.stat(filename).st_size
WindowsError: [Error 2] The system cannot find the file specified: "D:\\Library\\Daniel's_Books\\cover.jpg"
D:\Library\Daniel's_Books\cover.jpg
为什么反斜杠反斜杠? 我该如何解决这个错误?
【问题讨论】:
-
我不确定为什么它会添加额外的\。但是您是否尝试过使用“/”来代替?对于大多数库和语言,它通常对路径效果更好。
-
是的,返回完全相同的东西。完全正确。
-
听起来像 windows vista/7 和权限问题,因为您试图访问用户目录之外的文件...
标签: python windows file path size