Created
November 11, 2019 20:39
-
-
Save anderseknert/24c63df47cbc2756e05aca4ce566de81 to your computer and use it in GitHub Desktop.
Scan dir recursively and report total size of all files matching glob pattern
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 os | |
import glob | |
from pathlib import Path | |
def total_size(dir): | |
"""Scan dir recursively and report total size of all files matching glob pattern""" | |
total_size = 0 | |
last_reported = '' | |
for f in glob.iglob(dir, recursive=True): | |
#print(f) | |
total_size = total_size + os.path.getsize(f) | |
readable = sizeof_fmt(total_size) | |
if (readable != last_reported): | |
print(readable) | |
last_reported = readable | |
def sizeof_fmt(num, suffix='B'): | |
for unit in ['','Ki','Mi','Gi','Ti','Pi','Ei','Zi']: | |
if abs(num) < 1024.0: | |
return "%3.1f%s%s" % (num, unit, suffix) | |
num /= 1024.0 | |
return "%.1f%s%s" % (num, 'Yi', suffix) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment