very quickly getting total size of folder

Posted by freakazo on Stack Overflow See other posts from Stack Overflow or by freakazo
Published on 2010-03-21T02:54:56Z Indexed on 2010/03/21 3:01 UTC
Read the original article Hit count: 429

Filed under:
|
|
|

I want to quickly find the total size of any folder using python.

def GetFolderSize(path):
TotalSize = 0
for item in os.walk(path):
    for file in item[2]:
        try:
            TotalSize = TotalSize + getsize(join(item[0], file))
        except:
            print("error with file:  " + join(item[0], file))
return TotalSize

That's the simple script I wrote to get the total size of the folder, it took around 60 seconds (+-5 seconds). By using multiprocessing I got it down to 23 seconds on a quad core machine.

Using the Windows file explorer it takes only ~3 seconds (Right click-> properties to see for yourself). So is there a faster way of finding the total size of a folder close to the speed that windows can do it?

Windows 7, python 2.6 (Did searches but most of the time people used a very similar method to my own) Thanks in advance.

© Stack Overflow or respective owner

Related posts about python

Related posts about beginner