diff options
Diffstat (limited to 'gn3')
-rw-r--r-- | gn3/__init__.py | 0 | ||||
-rw-r--r-- | gn3/file_utils.py | 20 |
2 files changed, 20 insertions, 0 deletions
diff --git a/gn3/__init__.py b/gn3/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/gn3/__init__.py diff --git a/gn3/file_utils.py b/gn3/file_utils.py new file mode 100644 index 0000000..8e342c9 --- /dev/null +++ b/gn3/file_utils.py @@ -0,0 +1,20 @@ +"""Procedures that operate on files/ directories""" +import hashlib +import os + +from functools import partial + + +def get_dir_hash(directory: str) -> str: + """Return the hash of a DIRECTORY""" + md5hash = hashlib.md5() + if not os.path.exists(directory): + raise FileNotFoundError + for root, _, files in os.walk(directory): + for names in files: + file_path = os.path.join(root, names) + with open(file_path, "rb") as file_: + for buf in iter(partial(file_.read, 4096), b''): + md5hash.update(bytearray(hashlib.md5(buf).hexdigest(), + "utf-8")) + return md5hash.hexdigest() |