import easygui as g
import os
path = g.fileopenbox(default="*.txt")
with open(path) as old_file:
title = os.path.basename(path) #从路径中截取文件名
msg = "文件%s的内容如下:"%title
text = old_file.read()
text_after = g.textbox(msg, title, text)
if text != text_after[:-1]: #去除返回值的最后一个换行符
choice = g.buttonbox("检测到文件内容发生改变,请选择以下方式进行操作",
"警告",("保存","另存为","取消"))
if choice == "保存":
with open(path, "w") as old_file:
old_file.write(text_after[:-1])
if choice == "取消":
pass
if choice == "另存为":
path_new = g.filesavebox(default="*.txt")
if os.path.splitext(path_new)[1] != ".txt": #取得后缀名
path_new += ".txt"
with open(path_new, "w") as new_file:
new_file.write(text_after[:-1])
print(title)
浏览文本文件并进行修改保存
最新推荐文章于 2021-03-26 19:07:45 发布