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())
result_time = []
result_time.append(now_time[0])
result_time.append(now_time[1])
result_time.append(now_time[2])
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]
file.append(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])}日"
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("通知", "网邮回复模板已更新至最新日期!")
return 0
if __name__ == "__main__":
file_path = "D:/华师/就业办/网邮回复模板(勿改名称).txt"
file_path_2 = "./网邮回复模板(勿改名称).txt"
result_time = get_now_time()
in_file = get_file(file_path_2)
file = open(file_path_2, 'w').close()
out_file(in_file, result_time, file_path_2)
show_notice()