双文件同步遍历,生成md5

# -*- 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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值