Das folgende Snippet summiert rekursiv alle Dateigrößen eines als Kommandozeilenargument gegebenen Verzeichnisses (und zählt nebenbei die Summanden). Nicht zu verwechseln mit dem Output von 'du', das den verbrauchten Plattenplatz angibt, also eine größere Zahl und insbesondere nicht mit jedem Dateisystem die gleiche.
1 import sys, os
2
3 """
4 recursively counts all files and computes the sum of all filesizes in the given directory
5 """
6
7 thesum = 0
8 filecount = 0
9 for root, dirs, files in os.walk(sys.argv[1]):
10 abs_path = os.path.join(root, name)
11 size = os.path.getsize(abs_path)
12 thesum += sum(size for name in files)
13 filecount += len(files)
14
15 print "total of", sys.argv[1], ":", thesum, "bytes in", filecount, "files"