emulating du with os.walk

Dan Perl danperl at rogers.com
Sun Sep 26 19:59:33 EDT 2004


Firs of all, I don't know how much you already know about os.walk, but it 
can traverse trees either top-down or bottom-up (it has an argument 
'topdown').  The default is topdown=True.  What you probably need in your 
case is a bottom-up traversal (so pass topdown=False).

Then you have to keep track of all the directories (I can suggest a data 
structure if you want) and add the du values of all the children directories 
plus the sizes of all the files to determine the du value of a parent 
directory.

Without seeing your code, I'm guessing you are not doing one of these 
things.

Dan

"Kirk Job-Sluder" <kirk at eyegor.jobsluder.net> wrote in message 
news:slrnclej7g.cl6.kirk at eyegor.jobsluder.net...
> Hrm, I'm a bit stumped on this.
>
> I want to write a script lists a file directory hierarchy and gives me a
> sorted list showing cumulative directory size.  The example code for
> os.walk gets me half-way there, but I can't quite figure out how to do
> the hierarchal sum.  Here is the output I'm getting:
>
> /home/kirk/.gconf/apps/ggv/layout consumes 228 bytes in 1 non-directory
> files
> /home/kirk/.gconf/apps/ggv consumes 0 bytes in 1 non-directory files
> /home/kirk/.gconf/apps consumes 0 bytes in 1 non-directory files
>
> However, what I want is:
>
> /home/kirk/.gconf/apps/ggv/layout consumes 228 bytes in 1 non-directory
> files
> /home/kirk/.gconf/apps/ggv consumes 228 bytes in 1 non-directory files
> /home/kirk/.gconf/apps consumes 228 bytes in 1 non-directory files
>
> There should be an easy way to get around this, or perhaps I'm better
> off just parsing the output of du.
>
> -- 
> Kirk Job-Sluder
> "The square-jawed homunculi of Tommy Hilfinger ads make every day an
> existential holocaust."  --Scary Go Round 





More information about the Python-list mailing list