Skip to content

Instantly share code, notes, and snippets.

@ecatanzani
Created May 27, 2022 08:23
Show Gist options
  • Save ecatanzani/a048504ed4e1272299aad76e8954624e to your computer and use it in GitHub Desktop.
Save ecatanzani/a048504ed4e1272299aad76e8954624e to your computer and use it in GitHub Desktop.
sub-directory file size - recursive
import os
mb_scale_factor: float = pow(1024, 2) # If you want result expressed in MB
def get_dir_size(path: str) -> float:
total: int = 0
with os.scandir(path) as it:
for entry in it:
if entry.is_file():
total += entry.stat().st_size
elif entry.is_dir():
total += get_dir_size(entry.path)
return total/mb_scale_factor
main_folder: str = "path/to/main_folder"
with os.scandir(main_folder) as it:
for entry in it:
if entry.is_dir() and not entry.name.startswith("."):
print(f"{entry.name}: {round(get_dir_size(entry.path), 2)} MB")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment