我使用此代码(在Python 2.7.8中是为了在一个大目录中找到MB \ GB中的子文件夹大小:
# -*- 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.2fGB" % ((get_size(current))/1000) # this line do the same as the next line
print "---%0.1fGB---" % (conv_MB_to_GB(get_size(current))*1000)
print
此代码运行完美,但是如果希伯来语中有一个带有名称文件的文本文件,我会报错(如果名称文件是英语,那没有问题):
WindowsError: [Error 123] שם הקובץ,: 'G:\\PROJECTS\\mikta7\\gis\\?\xee\xf1\xee\xea \xe8\xf7\xf1\xe8 ?\xe7\xe3\xf9.txt'
我也是红色的how to read a file that can be saved as either ansi or unicode in python?
不幸的是,我不在安装了Python的计算机上,但是以下更改应该可以工作:
for f in filenames:
fp = os.path.join(dirpath, f.encode('utf-8'))
total_size += os.path.getsize(fp)
另外是将原始文件名编码为可识别的格式-在这种情况下为utf-8。请注意,这是encode
的默认参数,因此理论上可以省略。
有关更多参考,请参阅: