Python easyGUI 文件对比 覆盖保存

#在35-3的基础上进行优化,当用户点击ok按钮的时候,对打开的文件进行检查是否修改。
# 如果修改过,则提示覆盖保存、放弃保存、另存为并实现相应的功能

1
import easygui as g 2 import os 3 msg='浏览文件并打开' 4 title='测试' 5 default='D:Python练习*' 6 fileType='全部文件' 7 filePath=g.fileopenbox(msg,title,default,fileType) 8 9 with open(filePath) as f: 10 title=os.path.basename(filePath) 11 msg='文件%s的内容如下:'%title 12 txt=f.read() 13 txt_new=g.textbox(title,msg,txt) 14 15 if txt != txt_new[:-1]: 16 #检查文件是否修改,因为easygui,txtbox会在返回字符串后面追加一个行结束符(" "),因此在比较稳健师傅改变时,需要我们人工忽略这个行结束符 17 msg1='选择您的操作:' 18 title='检测到文件被修改,请选择是否保存:' 19 buttons=['覆盖保存','放弃保存','另存为'] 20 choice=g.buttonbox(title,msg,buttons) 21 #覆盖保存 22 if choice =='覆盖保存': 23 with open(filePath,'w') as f2: 24 f2.write(txt_new) 25 #放弃保存 26 if choice == '放弃保存': 27 pass 28 #另存为。。。 29 if choice == '另存为': 30 new_path=g.filesavebox(default='txt') 31 with open(new_path,'w') as f3: 32 f3.write(txt_new)
原文地址:https://www.cnblogs.com/scios/p/8386633.html