Skip to content

Instantly share code, notes, and snippets.

@anderseknert
Created November 11, 2019 20:39
Show Gist options
  • Save anderseknert/24c63df47cbc2756e05aca4ce566de81 to your computer and use it in GitHub Desktop.
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
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