Created
March 10, 2012 21:10
-
-
Save mithbre/2013215 to your computer and use it in GitHub Desktop.
Get a sha256 of every file in a directory, no recursion.
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
import os, sys, hashlib | |
def sha_list(fList): | |
for folder, items in fList: | |
for file in items: | |
sha = sha256(os.path.join(folder, file)) | |
write_file(folder, file, sha) | |
def crawl(argv): | |
origDir = os.getcwd() | |
fList = [] | |
for folder in argv: | |
f = [] | |
os.chdir(folder) | |
cwd = os.getcwd() | |
for item in os.listdir(cwd): | |
if os.path.isfile(item): | |
f.append(item) | |
fList.append([cwd, sorted(f)]) | |
os.chdir(origDir) | |
return fList | |
def sha256(path): | |
s = hashlib.sha256() | |
file = open(path, "rb") | |
buff_size = 65536 | |
while True: | |
data = file.read(buff_size) | |
if not data: | |
break | |
s.update(data) | |
file.close() | |
return s.hexdigest() | |
def write_file(folder, file, sha): | |
# * is a binary flag. Unix systems don't care, Windows does. | |
# chr(10), linefeed character | |
line = sha + " *" + file + chr(10) | |
sFile = os.path.join(folder, os.path.basename(folder) + ".sha2") | |
f = open(sFile, 'a') | |
f.write(str(line)) | |
f.close() | |
def main(argv): | |
sha_list(crawl(argv)) | |
if __name__ == "__main__": | |
main(sys.argv[1:]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment