"""Calculate the size of a directory and its sub-directories."""
import os
from pathlib import Path
from typing import Union
def get_size(path: Union[Path, str], include_dirs_size=True) -> int:
"""Return the size of a file or a directory in bytes."""
path = Path(path)
size = 0
if path.is_dir():
list_paths = path.glob("**/*")
elif path.is_file():
list_paths = [path]
else:
list_paths = []
for cur_path in list_paths:
if not include_dirs_size and cur_path.is_dir():
continue
if not cur_path.is_symlink():
size += cur_path.stat().st_size
return size
Code language: Python (python)