Created
May 14, 2025 21:54
-
-
Save madewokherd/b33a19d606209f38a430a396745381e3 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| #!/usr/bin/env python | |
| from __future__ import division, print_function | |
| import sys | |
| import os | |
| import heapq | |
| import time | |
| try: | |
| input = raw_input | |
| except NameError: | |
| pass | |
| items = [] | |
| t = 0 | |
| total_bytes = 0 | |
| total_files = 0 | |
| ignored = set() | |
| def add_files(path, dev=None): | |
| global t | |
| global total_bytes | |
| global total_files | |
| if time.time() > t: | |
| t = time.time() + 0.2 | |
| sys.stdout.write("\rScanning (%s files totaling %s seen so far)..." % (total_files, size_to_hr(total_bytes))) | |
| sys.stdout.flush() | |
| total_files += 1 | |
| try: | |
| statinfo = os.lstat(path) | |
| except OSError: | |
| return 0 | |
| if dev is None: | |
| dev = statinfo.st_dev | |
| elif statinfo.st_dev != dev: | |
| return 0 | |
| size = statinfo.st_size | |
| total_bytes += size | |
| if statinfo.st_mode & int('170000', 8) == int('40000', 8): | |
| # directory | |
| try: | |
| for subpath in os.listdir(path): | |
| size += add_files(os.path.join(path, subpath), dev) | |
| except OSError: | |
| pass | |
| heapq.heappush(items, (-size, path)) | |
| return size | |
| def add_items(): | |
| if len(sys.argv) >= 2: | |
| rootdir = sys.argv[1] | |
| else: | |
| default_rootdir = os.path.abspath(os.sep) | |
| rootdir = input("Enter path to examine [%s]: " % default_rootdir) | |
| if not rootdir: | |
| rootdir = default_rootdir | |
| print('') | |
| add_files(rootdir) | |
| print('') | |
| def size_to_hr(size): | |
| if size > 1048576: | |
| if size > 1073741824: | |
| return "%0.2fG" % (size / 1073741824) | |
| else: | |
| return "%0.2fM" % (size / 1048576) | |
| else: | |
| if size > 1024: | |
| return "%0.2fK" % (size / 1024) | |
| if size == 1: | |
| return "1 byte" | |
| return "%s bytes" % size | |
| add_items() | |
| while items: | |
| size, path = heapq.heappop(items) | |
| head = tail = os.path.normpath(path) | |
| while tail: | |
| head, tail = os.path.split(head) | |
| if head in ignored: | |
| break | |
| else: | |
| size = -size | |
| print() | |
| print("%s\t%s" % (size_to_hr(size), path)) | |
| while True: | |
| try: | |
| i = input("Expand/Ignore/# of parent levels to ignore/Quit ?").lower() | |
| except EOFError: | |
| sys.exit(0) | |
| if i == 'e': | |
| break | |
| elif i == 'i': | |
| ignored.add(os.path.normpath(path)) | |
| break | |
| elif i.isdigit(): | |
| for n in range(int(i)): | |
| path, _tail = os.path.split(path) | |
| ignored.add(os.path.normpath(path)) | |
| break | |
| elif i == 'q': | |
| sys.exit(0) | |
| break | |
| # else continue |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment