Я пытаюсь проверить размер 40 папок подкаталогов с помощью этого кода:
# -*- coding: cp1255 -*-
import os
def conv_MB_to_GB(input_megabyte):
gigabyte = 1.0/1000/1000
convert_gb = gigabyte * input_megabyte
return convert_gb
def get_size(start_path = "."):
total_size = 0
for dirpath, dirnames, filenames in os.walk(start_path):
for f in filenames:
fp = os.path.join(dirpath, f)
total_size += os.path.getsize(fp)
return total_size/(1024*1024.0)
rootPath = r"G:\PROJECTS"
os.chdir(rootPath)
all_subdirs = [ d for d in os.listdir('.') if os.path.isdir(d)]
for dirs in all_subdirs:
dir = os.path.join(rootPath, dirs)
os.chdir(dir)
current = os.getcwd()
print current
if get_size(current) < 1000:
print "%0.1fMB" % get_size(current)
if get_size(current) > 1000:
print "%0.1fGB" % (conv_MB_to_GB(get_size(current))*1000)
Проблема заключается в том, что возникает ошибка, когда имя подкаталога пишется справа налево:
Message File Name Line Position
Traceback
<module> G:\desktop\y\test.py 29
get_size G:\desktop\y\test.py 18
getsize C:\Python27\ArcGIS10.3\Lib\genericpath.py 49
exceptions.WindowsError: [Error 123] name,: 'G:\\PROJECTS\\redLineEast\\gis\\lyr\\26_7_17\\?\xee\xf1\xee\xea \xe8\xf7\xf1\xe8 ?\xe7\xe3\xf9.txt'