jeudi 5 novembre 2015

os.path.getsize unable to get size of subdirectories

I tried to make a script which will move all subdirectories if they are smaller than 400mb. I wrote this piece of code :

import os
import string
import re
import sys
import glob
import fileinput
import shutil

src = raw_input("Enter source disk location: ")
src = os.path.dirname(src) 
for dir,_,_ in os.walk(src): 
file_path = glob.glob(os.path.join(dir,"*.txt")) 
for file in file_path: 
    f = open(file, 'r')
    object_name = f.readlines()
    f.close()
part = os.path.dirname(os.path.dirname(file)) 
    part_file1 = os.path.basename(part)
    part_file2 = os.path.split(part)[0] 
    part_file3 = os.path.split(part)[1]
    source = os.path.join(part_file2, part_file3)
    path_small = os.path.join(part_file2, "small")
    file_source = glob.glob(os.path.join(source,dir))
    for element in file_source: 

        final_file_source = element
        #print "element", element
    if not os.path.exists(path_small):
        os.makedirs(path_small)
    print"element", element
    try:
        print (os.path.getsize(element))
    except Exeption, e:
        print str(e)
        pass

    if os.path.getsize(element) > 4000: #problem start here 
    # do sth 
    else:
        shutil.move(element,path_small)

The problem is that os.path.getsize give me a size of the file that describes that directory. How to get a size of sub-directories ? I checked on other answers and i found this example os.path.getsize Returns Incorrect Value? but i dont know how to aplly this on my code. :(

Aucun commentaire:

Enregistrer un commentaire