Created
May 27, 2022 08:23
-
-
Save ecatanzani/a048504ed4e1272299aad76e8954624e to your computer and use it in GitHub Desktop.
sub-directory file size - recursive
This file contains hidden or 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 | |
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