Bring value from walk()

Steve Holden sholden at bellatlantic.net
Fri Feb 25 10:03:50 EST 2000


Milos Prudek wrote:
> 
> I need a function that walks the dir tree and adds up occupied space.
> This is the source. "print Dirn", "print '-',X,Fs" and "print End of
> walk..." lines are for debugging only.
> 

Milos:

This isn't classed up, but might do what you want.
I'm sure it could be optimized: this is NOT production code.

regards
 Steve

def usage(path, names, verbose=0):
    sum = 0
    names.sort()
    dirs = []
    files = []
    for name in names:
        n = os.path.join(path,name)
        if os.path.isdir(n):
            dirs.append(n)
        else:
            files.append(n)
    for d in dirs:
        sum = sum + usage(d, os.listdir(d), verbose)
    for f in files:
	l = os.path.getsize(f)
        sum = sum + l
        if verbose: print "%s %d" % (f, l)
    
    print "%s %d" % (path, sum)
    return(sum)


--
"If computing ever stops being fun, I'll stop doing it"



More information about the Python-list mailing list