DISK USAGE CALCULATOR
Python
#!/usr/bin/env python3
"""DISK USAGE CALCULATOR"""
import os, tempfile
temp = tempfile.gettempdir()
test_dir = os.path.join(temp, "disk_test")
os.makedirs(os.path.join(test_dir, "subdir"))
for i in range(5):
open(os.path.join(test_dir, f"file{i}.txt"), 'w').write("x" * 100)
open(os.path.join(test_dir, "subdir/big.txt"), 'w').write("x" * 500)
def get_size(path):
total = 0
for root, dirs, files in os.walk(path):
for file in files:
filepath = os.path.join(root, file)
total += os.path.getsize(filepath)
return total
size = get_size(test_dir)
print(f"Total size: {size} bytes ({size/1024:.2f} KB)")
import shutil
shutil.rmtree(test_dir)