0

I am new to Python and I need to get the size from a directory with more than 500k files. I found some code on the internet which should be really fast. Somehow it doesnt work and I dont know why. The current output is only 'Test' so it doesnt even enter the function.

import time
import os

start_time = time.time()
print('Test')
def getSize(path):
    print('Test2')
    total = 0
    for entry in os.scandir(path):
        if entry.is_dir(follow_symlinks=False):
            total += getSize(entry.path)
        else:
            total += entry.stat(follow_symlinks=False).st_size
    return total

    print (float(getSize('U:\Java'))/1024/1024/1024)
    print("--- %s Sekunden ---" % round(time.time() - start_time, 2))

Additional question: Is there an even faster way to search such big directories with python or other languages?

wenzel267
  • 93
  • 1
  • 12
  • 1
    What about http://stackoverflow.com/questions/1392413/calculating-a-directory-size-using-python – Paul Rooney May 04 '17 at 11:30
  • hm, do you actually call the function? like `getSize('path/to/filder')` as a last line. – j-i-l May 04 '17 at 11:31
  • I already tried that one. I also tried this one. http://stackoverflow.com/a/2485804/7862001the second one is faster than the method with os.walk. – wenzel267 May 04 '17 at 11:34

1 Answers1

2

The two last lines in your program, where you call the function, are indented and therefore considered a part of the function and will not execute. Simply dedent them.

Uriel
  • 15,579
  • 6
  • 25
  • 46