By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,089 Members | 2,418 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,089 IT Pros & Developers. It's quick & easy.

Getting directory size

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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: n/a
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

P: n/a
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.