# -*- coding: UTF-8 -*-
"""
@Time : 2022/8/10 14:25
@Author : JiaoHiacheng
@Email : JHC000abc@gmail.com
@Project : Python
@File : make_match_md5.py
@des : 根据文件,按行生成对应的 md5 文件
"""
import os
from datetime import datetime
import hashlib
def gen_md5_id(item):
item = item.rstrip("\n")
md5_machine = hashlib.md5()
md5_machine.update(item.encode('utf-8'))
return md5_machine.hexdigest()
def check_exists(file):
if not os.path.exists(file):
os.makedirs(file)
def make_md5(in_file, out_file):
print("目标文件:{}\n输出位置:{}".format(in_file, out_file))
print("开始生成:{}".format(datetime.now()))
in_name = os.path.split(in_file)[1]
# MD5名字
out_file_name = "md5_" + in_name
check_exists(out_file)
file_pointer = 0
out_url = os.path.join(out_file, out_file_name)
if os.path.exists(out_url):
os.remove(out_url)
print("{} 已经存在,影响操作,已删除原始文件".format(out_url))
else:
pass
f_out = open(out_url, "a", encoding="utf-8")
f_in = open(in_file, "r", encoding="utf-8")
f_line = f_in.readline()
while f_line:
file_pointer += 1
md5_out = gen_md5_id(f_line)
f_out.write(md5_out + "\n")
f_line = f_in.readline()
f_in.close()
f_out.close()
print("结束生成:{}".format(datetime.now()))
print("文件总行数:{}".format(file_pointer))
if __name__ == '__main__':
in_file = R"D:\Desktop\test\zh_jp_rq.txt"
out_file = R"D:\Desktop\test\md5_file"
make_md5(in_file, out_file)
双文件同步遍历,生成md5
最新推荐文章于 2023-04-23 10:13:23 发布