Skip to content

Instantly share code, notes, and snippets.

@mortie
Created March 4, 2022 08:35
Show Gist options
  • Save mortie/35fdaf022cfaa1314f0dfc3bc8885424 to your computer and use it in GitHub Desktop.
Save mortie/35fdaf022cfaa1314f0dfc3bc8885424 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
from pathlib import Path
import time
import sys
dirpath = sys.argv[1]
def fsize(f):
try:
return f.stat().st_size
except:
return 0
def sum_dir(path):
while True:
try:
return sum(fsize(file) for file in Path(path).rglob('*'))
except: pass
def pretty_size(size):
sz = abs(size)
if sz > 1024 * 1024 * 1024:
return f"{size / (1024 * 1024 * 1024)} GiB"
elif sz > 1024 * 1024:
return f"{size / (1024 * 1024)} MiB"
elif sz > 1024:
return f"{size / 1024} kiB"
else:
return f"{size} B"
def calc_rate(duration, delta_size):
bps = delta_size / duration
return f"{pretty_size(bps)}/s"
prev_size = sum_dir(dirpath)
prev_time = time.time()
print(pretty_size(prev_size))
while True:
time.sleep(4)
size = sum_dir(dirpath)
t = time.time()
print(pretty_size(size), calc_rate(t - prev_time, size - prev_size))
prev_size = size
prev_time = t
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment