Getting directory size

F

francisl

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.

Thanks
 
P

Peter Hansen

francisl said:
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
 
G

Graham Fawcett

Peter said:
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.

With Orendorff's path module, this becomes a two-liner:

from path import path
dir_size = sum([f.size for f in path('mydir').walkfiles()])

With Python 2.4 genexps, you don't even need the square brackets...

http://www.jorendorff.com/articles/python/path/

Shoulda-been-added-to-the-standard-library'ly yours,

-- Graham
 
F

francisl

Graham said:
Peter said:
directory


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.


With Orendorff's path module, this becomes a two-liner:

from path import path
dir_size = sum([f.size for f in path('mydir').walkfiles()])

With Python 2.4 genexps, you don't even need the square brackets...

http://www.jorendorff.com/articles/python/path/

Shoulda-been-added-to-the-standard-library'ly yours,

-- Graham

Thank you all

Before I received any anwser, I wrote a recursive function that walk into directory adding file size with os.getsize().
But I was stop because the file have an average of 15 go. And the object return that the size is too large for valid assignation.

I have to use 1.5.2 at work, because it is the only version available on our vmware server.

So I thing I will have to try popen.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,222
Messages
2,571,142
Members
47,775
Latest member
MadgeMatti

Latest Threads

Top