Getting directory size

Peter Hansen peter at engcorp.com
Mon Mar 21 09:57:41 EST 2005


francisl wrote:
> How can we get a full directory size (sum of all his data)?
> like when we type `du -sh mydir`
> 
> Because os.path.getsize('mydir') only give the size of the directory 
> physical representation on the disk.

os.popen('du -sh mydir') would be one approach.

The harder way is to use os.walk('mydir') to scan all
files in all subdirectories, and use os.stat() or
os.path.getsize() to add up the sizes of each file.

-Peter



More information about the Python-list mailing list