python实现自动更新txt文档中的日期

python实现自动更新txt文档中的日期

"""
文件:python100题之220726_1
时间:2022-07-26
作者:wangRui
题目描述:自动更新jyb回复模板的时间
"""

import requests
import os
import time
from plyer import notification
import tkinter.messagebox


def get_now_time():
    now_time = time.localtime(time.time())
    # print(type(now_time))
    result_time = []
    result_time.append(now_time[0])
    result_time.append(now_time[1])
    result_time.append(now_time[2])
    # print(type(now_time[1]))
    return result_time


def num_to_chinese(num):
    num_chinese = {1: "一", 2: "二", 3: "三", 4: "四", 5: "五",
                   6: "六", 7: "七", 8: "八", 9: "九", 10: "十",
                   11: "十一", 12: "十二", 13: "十三", 14: "十四", 15: "十五",
                   16: "十六", 17: "十七", 18: "十八", 19: "十九", 20: "二十",
                   21: "二十一", 22: "二十二", 23: "二十三", 24: "二十四", 25: "二十五",
                   26: "二十六", 27: "二十七", 28: "二十八", 29: "二十九", 30: "三十",
                   31: "三十一",
                   2022: "二〇二二", 2023: "二〇二三", 2024: "二〇二四", 2025: "二〇二五", 2026: "二〇二六",
                   2027: "二〇二七", 2028: "二〇二八", 2029: "二〇二九", 2030: "二〇三〇", 2031: "二〇三一",
                   2032: "二〇三二", 2033: "二〇三三", 2034: "二〇三四", 2035: "二〇三五", 2036: "二〇三六",
                   2037: "二〇三七", 2038: "二〇三八", 2039: "二〇三九", 2040: "二〇四〇", 2041: "二〇四一",
                   2042: "二〇四二", 2043: "二〇四三", 2044: "二〇四四", 2045: "二〇四五", 2046: "二〇四六",
                   }
    return num_chinese[num]


def get_file(file_path):
    file = []
    with open(file_path, encoding='utf-8') as fin:
        for line in fin:
            line = line[:-1]
            # print(line)
            file.append(line)
            # print(line)
    return file


def update_date(result_time):
    line = f"{num_to_chinese(result_time[0])}年" \
           f"{num_to_chinese(result_time[1])}月" \
           f"{num_to_chinese(result_time[2])}日"
    # print(line)
    return line


def out_file(in_file, result_time, file_path_2):
    with open(file_path_2, "a", encoding='utf-8') as fout:
        for line in in_file:
            # 如果是含有日期的那一行,那么进行替换
            if "二〇" in line:
                line = update_date(result_time)
            fout.write(line + "\n")


def show_notice():
    # 以下的通知是电脑正中心的弹出框通知
    tkinter.messagebox.showinfo("通知", "网邮回复模板已更新至最新日期!")

    # 以下的通知是桌面右下角的任务栏通知
    # notification.notify(
    #     title="通知",
    #     message="网邮回复模板已更新至最新版",
    #     app_icon="",
    #     timeout=5
    # )

    return 0


if __name__ == "__main__":
    # file_path是绝对路径
    # file_path_2是相对路径
    file_path = "D:/华师/就业办/网邮回复模板(勿改名称).txt"
    file_path_2 = "./网邮回复模板(勿改名称).txt"

    result_time = get_now_time()
    in_file = get_file(file_path_2)

    # 将原有的txt文件删除
    file = open(file_path_2, 'w').close()

    out_file(in_file, result_time, file_path_2)
    show_notice()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值