I’d like to create python command line code that is able to print directory tree with sizes of all subdirectories (from certain directory) and most frequent extensions… I will show the example output.
- root_dir (5 GB, jpg (65 %): avi ( 30 %) : pdf (5 %))
— aa (3 GB, jpg (100 %) )
— bb (2 GB, avi (20 %) : pdf (2 %) )
— bbb (1 GB, …)
— bb2 (1 GB, …)
— cc (1 GB, pdf (100 %) )
The format is :
nesting level, directory name (size of the directory with all files and subdirectories, most frequent extensions with size percentages in this directory.
I have this code snippet so far. The problem is that it counts only file sizes in directory, so the resulting size is smaller than real size of the directory. Other problem is how to put it all together to print the tree I defined above without redundant computations.
Calculating directory sizes really isn’t python’s strong suit, as explained in this post: very quickly getting total size of folder. If you have access to
duandfind, by all means use that. You can easily display the size of each directory with the following line:If you insist in doing this in python, you may prefer post-order traversal over
os.walk, as suggested by PableG. But usingos.walkcan be visually cleaner, if efficiency is not the utmost factor for you: