Skip to content

Instantly share code, notes, and snippets.

@ffoxin
Created February 7, 2014 10:35
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 ffoxin/8860408 to your computer and use it in GitHub Desktop.
Save ffoxin/8860408 to your computer and use it in GitHub Desktop.
Create a snapshot of directory (including subdirs) for latest comparison
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import hashlib
import os
def print_dir(path, size=False, md5=False):
"""
:type path: str
"""
file_format = '{}{}'
size_format = ' (size: {})'
md5_format = ' (md5: {})'
if os.path.exists(path):
def_indent = os.path.normpath(path).count(os.sep)
for root, dirs, files in os.walk(path):
indent = (root.count(os.sep) - def_indent) * 2
print('{}[{}]'.format(' ' * indent, os.path.basename(root)))
indent += 2
for f in files:
print_format = file_format.format(' ' * indent, f)
full_path = os.path.join(root, f)
if size:
print_format += size_format.format(os.path.getsize(full_path))
if md5:
with open(full_path, 'rb') as file:
digest = hashlib.md5(file.read()).hexdigest()
print_format += md5_format.format(digest)
print(print_format)
print('Finished')
else:
print('Error: cant find path')
if __name__ == '__main__':
target = 'd:\\projects\\'
print_dir(target, size=True, md5=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment