Skip to content

Instantly share code, notes, and snippets.

@zaxcie
Last active December 21, 2017 04:52
Show Gist options
  • Save zaxcie/bd67bced31f60e593fa2327afa4e0c89 to your computer and use it in GitHub Desktop.
Save zaxcie/bd67bced31f60e593fa2327afa4e0c89 to your computer and use it in GitHub Desktop.
Recursivly generate a cache of SHA256 of a folder
import os
import hashlib
def generate_cache_recursive(path, cache):
'''Recursivly generate the cache of files in path. SHA256 is used.
path - string that is the path os a folder
cache - set that is the cache
return set which is the cache
'''
if os.path.isdir(path):
for item in os.listdir(path):
new_path = path + item
if new_path[-1] != "/" and os.path.isdir(new_path):
new_path = new_path + "/"
cache = generate_cache_recursive(new_path, cache)
elif os.path.isfile(path):
with open(path, 'rb') as file:
_hash = hashlib.sha256()
content = file.read()
_hash.update(content)
hash_output = _hash.hexdigest()
cache.update([hash_output])
return cache
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment