469,306 Members | 2,428 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,306 developers. It's quick & easy.

Getting directory size

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
Jul 18 '05 #1
4 21018
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
Jul 18 '05 #2
Peter Hansen wrote:
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.


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

Jul 18 '05 #3
P
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.


Have a look at:
http://www.pixelbeat.org/scripts/dutop

Pádraig.
Jul 18 '05 #4
Graham Fawcett wrote:
Peter Hansen wrote:
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.

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.
Jul 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Nate Harel | last post: by
17 posts views Thread by Arnold | last post: by
11 posts views Thread by KarimL | last post: by
2 posts views Thread by Raed Sawalha | last post: by
4 posts views Thread by Von Thep via DotNetMonster.com | last post: by
2 posts views Thread by mail2ganeshguru | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.