I use this code (in Python 2.7.8) in order to find a sub folder size in MB \ GB in one big directory:
# -*- 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
this code run perfectly, but if there is a text file with file name in Hebrew, i got an error (if the name file is english-there's no problem):
WindowsError: [Error 123] שם הקובץ,: 'G:\\PROJECTS\\mikta7\\gis\\?\xee\xf1\xee\xea \xe8\xf7\xf1\xe8 ?\xe7\xe3\xf9.txt'
i also red how to read a file that can be saved as either ansi or unicode in python?