您的位置:首页 > 编程语言 > Python开发

Python 修改目录下所有文件名为MD5

2017-09-25 21:55 791 查看
import os
import hashlib

def file_md5(file_name, block_size=2**20):
if not os.path.isfile(file_name):
return
hash = hashlib.md5()
with open(file_name, 'rb') as f:
while True:
b = f.read(block_size)
if not b:
break
hash.update(b)
return hash.hexdigest()

for root, dirs, files in os.walk("your_path"):
for file in files:
file_path = os.path.join(root, file)
md5 = file_md5(file_path)
print file_path, md5
if md5:
os.rename(file_path, os.path.join(root, md5))
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐