Created
July 8, 2013 10:24
-
-
Save cjlano/5947752 to your computer and use it in GitHub Desktop.
List directory tree structure sorted by file size using Python
Based on http://stackoverflow.com/questions/9727673/list-directory-tree-structure-using-python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def list_files(startpath): | |
for root, dirs, files in os.walk(startpath): | |
level = root.replace(startpath, '').count(os.sep) | |
indent = ' ' * 4 * (level) | |
print('{}{}/'.format(indent, os.path.basename(root))) | |
subindent = ' ' * 4 * (level + 1) | |
for f in sorted(files, key=lambda f: os.path.getsize(root + os.sep + f)): | |
print('{}{} - {}B'.format(subindent, f, os.path.getsize(root + os.sep + f))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment