Last active
November 18, 2018 19:26
-
-
Save hughdbrown/0b8770d93524b9701205e11fe68d3c93 to your computer and use it in GitHub Desktop.
Dump directory of files as text with useful file attributes for backup
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
#!/usr/bin/env python | |
import os | |
from datetime import datetime | |
from hashlib import sha1 | |
OMITTED_FILES = set([".DS_Store"]) | |
def sha1file(filename): | |
sh = sha1() | |
with open(filename, "rb") as handle: | |
while True: | |
buf = handle.read(1024 * 1024) | |
if not buf: | |
break | |
sh.update(buf) | |
return sh.hexdigest() | |
def main(): | |
for root, _, files in os.walk('.'): | |
for f in set(files) - OMITTED_FILES: | |
fullpath = os.path.join(root, f) | |
if os.path.isfile(fullpath): | |
yield { | |
"name": os.path.normpath(fullpath), | |
"size": os.path.getsize(fullpath), | |
"sha1": sha1file(fullpath), | |
"date": str(datetime.fromtimestamp(os.path.getmtime(fullpath))), | |
} | |
if __name__ == '__main__': | |
for d in main(): | |
print(d) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment