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

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to