|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
import easygui as g
import os
file_path = g.fileopenbox(default="*.txt")
with open(file_path) as old_file:
title = os.path.basename(file_path)
msg = "文件【%s】的内容如下:" % title
text = old_file.read()
text_after = g.textbox(msg,title,text)
if text != text_after:
choice = g.buttonbox('检测到文件内容发生改变,请选择以下操作','警告',choices=('覆盖保存','放弃保存','另存为'))
if choice =='覆盖保存':
with open(file_path,"w") as old_file:
old_file.write(text_after)
elif choice =='放弃保存':
pass
else:
another_path = g.filesavebox(default=".txt")
if os.path.splitext(another_path)[1] != '.txt':
another_path += '.txt'
with open(another_path,"w") as new_file:
new_file.write(text_after)
text_after不就是读取text显示的内容吗?这两个不是一直都一样吗?什么时候才会不一样?
本帖最后由 jackz007 于 2019-11-20 14:14 编辑
因为 g.textbox() 所显示的 text 的内容是可以编辑的。所以,text_after 是完全有可能是与原始文件内容 text 不相同的。语句 if text != text_after 实际上就是检测用户是否对 text 的文本内容进行了修改。
|
|