0

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?

newGIS
  • 598
  • 3
  • 10
  • 26

1 Answers1

0

Unfortunately I can't test this as I'm not on a machine with Python installed, but the following change should work:

for f in filenames:
    fp = os.path.join(dirpath, f.encode('utf-8'))
    total_size += os.path.getsize(fp)

The addition is to encode the raw filename in a recognisable format - in this case, utf-8. Note that this is the default parameter for encode so it can theoretically be omitted.

For further references, see:

Python docs - encode

user483934
  • 387
  • 1
  • 2
  • 8
  • i get an error: UnicodeDecodeError: 'ascii' codec can't decode byte 0xf2 in position 9: ordinal not in range(128) – newGIS Feb 19 '20 at 07:55