SHARE
TWEET
Untitled
a guest
Dec 19th, 2015
58
Never
- #USAGE: md5-convert.py "directory_with_files" ".csv file"
- import hashlib
- import codecs
- import sys
- import os
- import re
- import time
- DIR = sys.argv[1]
- CSV_FILE= sys.argv[2]
- count = 0;
- #gets md5 hash of file
- def md5(fname):
- hash = hashlib.md5()
- with open(fname, "rb") as f:
- for chunk in iter(lambda: f.read(4096), b""):
- hash.update(chunk)
- return hash.hexdigest()
- f = codecs.open(CSV_FILE, encoding='utf-8', mode='r')
- data = f.read()
- f.close()
- with codecs.open(CSV_FILE, encoding='utf-8', mode='r') as f:
- for line in f:
- line = line[:-1]
- line = line.split('\t')
- ext = re.search("\.jpg|\.jpeg|\.png|\.gif|\.webm", line[2]).group(0)
- hash = line[1]
- #construct absolute path for the md5 function
- path = sys.argv[1] + '\\'+ hash + ext
- #check to make sure the file exists
- if os.path.isfile(path) and os.access(path,os.R_OK):
- filehash = md5(path)
- newdata = data.replace(hash,filehash)
- #write data to temp file
- f2 = codecs.open('temp.csv',encoding='utf-8', mode='w')
- f2.write(newdata)
- f2.close()
- #set old data to the newdata
- data = newdata
- count+=1
- print "line #{count} converted to md5.".format(count = count)
- #time.sleep(.250)
- print "All hashes have been converted."
RAW Paste Data
