我試圖讓函數獲取目錄大小。路徑突然得到反斜槓
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