Python 计算某个目录下的文件md5并且以md5批量重命名

时间:2022-07-28 11:37:45
 1 #!/usr/bin/python
 2 # -*- coding:utf-8 -*-
 3 # Filename:getmd5&rename.py
 4 '''
 5 @author by imjc
 6 Created on 2013-12-25
 7 '''
 8 
 9 import os
10 import sys
11 import md5
12 
13 #修改文件名以命名md5
14 def getFileMd5():
15     thisPath = os.getcwd() + '\\allFile'    #获取allFile的路径
16     fileList = os.listdir(thisPath)
17     for i in fileList:
18         upPath = thisPath + '\\' + i    #获取allFile目录下文件路径
19         fileMd5 = md5.new()             #计算MD5
20         fileOpen = open(upPath, 'rb')
21         fileMd5.update(fileOpen.read())
22         fileOpen.close()
23         fileMd5List = fileMd5.hexdigest()
24         os.rename(upPath,os.path.join(thisPath,fileMd5List))    #把allFile目录下的文件以md5重命名
25 
26 # main    
27 if __name__ == "__main__":
28     getFileMd5()

本人为Python新手,如有错误请指点,谢谢。