Getting the directory size

Robert Roy rjroy at takingcontrol.com
Thu Jun 1 23:56:44 EDT 2000


On Thu, 01 Jun 2000 13:24:46 +0100, Steve Tregidgo <smst at bigfoot.com>
wrote:

>Pieter Claerhout wrote:
>> 
>> <code>
>[rest of code snipped]
>> def main():
>> 
>>         dir = sys.argv[1]
>> 
>>         print "Testing directorySize..."
>>         print "Directory: %s" %(dir)
>>         print "Size: %s %s" %(getDirSize(dir)[0], getDirSize(dir)[1])
>> 
>> if __name__ == '__main__':
>>         main()
>> </code>
>> 
>> Is there any way to fasten up this beauty, because depending on the
>> number of directories it has to walk through, it takes a long time. Is
>> there maybe a function in the win32 modules who does this for me,
>> but lots faster?
>> 
>I don't know about a dedicated function to do that, but you can double
>the speed by changing the last line from:
>
>    print "Size: %s %s" %(getDirSize(dir)[0], getDirSize(dir)[1])
>
>...to the pair of lines:
>
>    amount, units = getDirSize(dir)
>    print "Size: %s %s" % (amount, units)
>

if getDirSize returns a 2-tuple can simply do 

print "Size: %s %s" %getDirSize(dir)

or slice it if its a longer tuple

print "Size: %s %s" %getDirSize(dir)[:2]

:-)

Bob
>The original version walked the entire structure twice -- once to
>calculate the amount, and then again to give the units.  Not really what
>you were after, but faster nonetheless.
>
>> Kind regards,
>> 
>> Pieter Claerhout
>> 
>
>HTH a little,
>Steve
>
>-- 
>Steve Tregidgo
>Software Developer
>http://www.businesscollaborator.com
>




More information about the Python-list mailing list