|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
文件我没有进行改变,直接按ok,也是会显示文件发生改变,代码如下
- import easygui as g
- import os
- fpath=g.fileopenbox(default='*.txt')
- with open(fpath,encoding='utf-8') as old_f:
- title=os.path.basename(fpath)
- msg='文件【%s】的内容为:'% title
- text=old_f.read()
- text1=g.textbox(msg,title,text)
- if text != text1[:-1]: #textbox()的返回值会自动追加一个\n
- choice=g.buttonbox('文件发生改变','警告',('覆盖保存','放弃保存','另存为'))
- if choice=='覆盖保存':
- with open(fpath,'w',encoding='utf-8') as old_f:
- old_f.write(text1[:-1])
- if choice=='放弃保存':
- pass
- if choice=='另存为':
- newpath=g.filesavebox(default='*.txt')
- if os.path.splitext(newpath)[1] !='.txt':
- newpath += '.txt'
- with open(newpath,'w',encoding='utf-8') as new_f:
- new_f.write(text1[:-1])
复制代码
if text != text1[:-1]: #textbox()的返回值会自动追加一个\n,问题出在这一句,实际上不是所有的情况下textbox()都会追加\n,我的就不会追加,改成if text != text1就可以了。
|
|