diff --git a/sen2chain/utils.py b/sen2chain/utils.py index 18cc4ce223dd24b749e4a95674534a4f6ac5c00f..02c33d049b192e2b40d481b181e03dfefa05ebf7 100644 --- a/sen2chain/utils.py +++ b/sen2chain/utils.py @@ -4,6 +4,7 @@ This module contains usefull functions commons to every modules. """ +import os import logging from itertools import zip_longest from datetime import datetime @@ -70,3 +71,22 @@ def datetime_to_str(date: datetime, date_format: str) -> str: except KeyError: raise ValueError("Invalid date format") return datetime.strftime(date, date_format) + + +def human_size(bytes, units=['B','KB','MB','GB','TB', 'PB', 'EB']): + """ Returns a human readable string reprentation of bytes""" + return str(bytes) + units[0] if bytes < 1024 else human_size(bytes>>10, units[1:]) + +def getFolderSize(folder, follow_symlinks = False): + total_size = os.path.getsize(folder) + try: + for item in os.listdir(folder): + itempath = os.path.join(folder, item) + if os.path.isfile(itempath) and (follow_symlinks or not os.path.islink(itempath)): + total_size += os.path.getsize(itempath) + elif os.path.isdir(itempath) and (follow_symlinks or not os.path.islink(itempath)): + total_size += getFolderSize(itempath) + except: + pass + return total_size +