Created
March 31, 2018 14:30
-
-
Save lordkebab/23ebb9c2be5b8667d373bfee6c0ca648 to your computer and use it in GitHub Desktop.
Find the top 10 largest files in a list of file systems or directories
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
import sys | |
from pathlib import Path | |
from os.path import getsize | |
if __name__ == '__main__': | |
paths = sys.argv[1:] | |
for path in paths: | |
sizes = {} | |
try: | |
p = Path(path) | |
except: | |
print("{} is not a valid path".format(path)) | |
break | |
# recursively get the size of each file in the current path | |
for f in p.glob('**/*'): | |
# figure out if the current descriptor is a directory or file | |
try: | |
directory = f.is_dir() | |
except: | |
# sometimes windows for some reason can't access stuff because windows | |
break | |
if not directory: | |
fd = f.as_posix() # path as a string | |
sz = 0 # to avoid referencing before assignment | |
try: | |
sz = getsize(fd) | |
except: | |
# mostly for windows, if we get here the OS doesn't think the file exists | |
pass | |
sizes[fd] = sz | |
# print the top 10 files by file size | |
top10 = sorted(sizes.items(), key=lambda t: t[1], reverse=True) | |
print("Top 10 files in {}\n----------------------".format(path)) | |
print(top10[:9]) | |
print("\n") |
Note this is only for python 3
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
python largefiles.py /etc /home /opt