轉:!@¥%¥%
Python:
# -*- coding: utf-8 -*-
import os
import hashlib
from time import clock as now
def getmd5(filename):
file_txt = open(filename,'rb').read()
m = hashlib.md5(file_txt)
return m.hexdigest()
def main():
path = input("path: ")
all_size = {}
total_file=0
total_delete=0
start=now()
for file in os.listdir(path):
print(file)
total_file += 1
real_path=os.path.join(path,file)
if os.path.isfile(real_path) == True:
size = os.stat(real_path).st_size
name_and_md5=[real_path,'']
if size in all_size.keys():
new_md5 = getmd5(real_path)
if all_size[size][1]=='':
all_size[size][1]=getmd5(all_size[size][0])
if new_md5 in all_size[size]:
print ('刪除',file)
os.remove(real_path)
total_delete += 1
else:
all_size[size].append(new_md5)
else:
all_size[size]=name_and_md5
x = 1
for file in os.listdir(path):
new_name = 'pic' + str(x) + '.jpg'
os.rename(os.path.join(path,file),os.path.join(path,new_name))
x += 1
end = now()
time_last = end - start
print ('文件總數:',total_file)
print ('刪除個數:',total_delete)
print ('耗時:',time_last,'秒')
if __name__=='__main__':
main()
MD5,sha1的加密:Python 2.7
import hashlib
import os
def getmd5andsha1(filepath):
if os.path.isfile(filepath):
file_txt = open(filepath, 'rb').read()
else:
file_txt = filepath
m = hashlib.md5(file_txt)
mm = hashlib.sha1(file_txt)
return m.hexdigest(), mm.hexdigest()
if __name__ == '__main__':
s = raw_input('input file path or a string:')
md5_str, shal_str = getmd5andsha1(s)
print 'md5:', md5_str.upper()
print 'sha1:', shal_str.upper()
raw_input('press <Enter> to exit....')