Skip to content

Instantly share code, notes, and snippets.

@jansichermann
Created December 16, 2014 02:02
Show Gist options
  • Save jansichermann/b28c13ac841725a92a94 to your computer and use it in GitHub Desktop.
Save jansichermann/b28c13ac841725a92a94 to your computer and use it in GitHub Desktop.
Renames files in a folder after their creation time + md5 hash
#!/usr/bin/env python
import os
import time
import hashlib
def hashforfile(f):
md5 = hashlib.md5();
while True:
data = f.read(1024 * 1024)
if not data:
break
md5.update(data)
return md5.hexdigest()
if __name__ == '__main__':
folderPath = "/Volumes/bookI/jan/media/photos/";
for folder in os.walk(folderPath):
for filename in folder[2]:
path = folder[0] + "/" + filename
t = time.gmtime(os.path.getctime(path))
ts = "" + str(t[0]) + str(t[1]) + str(t[2]) + str(t[3]) + str(t[4]) + str(t[5])
extension = os.path.splitext(path)[1]
f = open(path, 'rb')
h = hashforfile(f);
f.close()
newPath = folder[0] + "/" + ts + "__" + h + extension.lower();
print path + "\t|\t" + newPath
os.rename(path, newPath)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment