Created
February 7, 2014 10:35
-
-
Save ffoxin/8860408 to your computer and use it in GitHub Desktop.
Create a snapshot of directory (including subdirs) for latest comparison
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 | |
# -*- 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