我试图让函数获取目录大小。路径突然得到反斜杠
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
为什么它反斜杠反斜杠? 以及如何修复错误?
我不确定为什么会添加额外的\。但是你有没有试过用“/”来代替?对于大多数图书馆和语言来说,它通常更适合路径。 – TheSHEEEP
是的,返回完全一样的东西。究竟。 – mirandalol
听起来像Windows Vista/7和权限问题,因为你试图访问你的userdir之外的文件... – mensi