Skip to content

Instantly share code, notes, and snippets.

@hughdbrown
Last active November 18, 2018 19:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hughdbrown/0b8770d93524b9701205e11fe68d3c93 to your computer and use it in GitHub Desktop.
Save hughdbrown/0b8770d93524b9701205e11fe68d3c93 to your computer and use it in GitHub Desktop.
Dump directory of files as text with useful file attributes for backup
#!/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