Created
October 22, 2011 02:19
-
-
Save kenany/1305450 to your computer and use it in GitHub Desktop.
Renames files based on their SHA1 hash. Meant for images.
This file contains 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 | |
''' | |
USE AT YOUR OWN RISK! | |
This script occasionally corrupts my files, this might be an error in the renaming process. | |
I only use this script for a bunch of meaningless photos that I don't mind losing. | |
You have been warned! | |
''' | |
__author__ = "Kenan Yildirim" | |
import os | |
import time | |
import sys | |
from hashlib import sha1 | |
def shaChecksum(filePath): # Modified version of snippet found here: http://joelverhagen.com/blog/2011/02/md5-hash-of-file-in-python | |
fh = open(filePath, 'rb') | |
m = sha1() | |
while True: | |
data = fh.read(8192) | |
if not data: | |
break | |
m.update(data) | |
return m.hexdigest() | |
def main(): | |
start = time.time() | |
for fname in os.listdir('.'): | |
fileExtension = fname[(len(fname) - 4):] # Obviously assuming a four character extension (including the dot) | |
if '.py' not in fileExtension: | |
shaHash = shaChecksum(fname)[:7] | |
print 'Converting ' + fname + ' to ' + shaHash + fileExtension | |
os.rename(fname, shaHash + fileExtension) | |
print 'Done in %.3f seconds.' % (time.time() - start) | |
if __name__ == '__main__': | |
sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment