|
发表于 2020-3-29 11:14:08
|
显示全部楼层
看你的问题很有可能是没注意到textbox方法会在文本末尾默认追加一个换行符'\n'所以你比较的时候需要给另一个对象加上换行符之后进行比较。
其次,并没有打开三个文件的必要,这样显得代码的思路非常复杂,需要保存文本的话可以把文本存放到字符串中进行处理。
这是我当时学的时候的,有可能有些遗漏,不过只是作为参考的话应该还是能跑的。
- import easygui as g
- import os
- filedir = g.fileopenbox()
- with open(filedir) as f:
- string = ''
- for each_line in f:
- string += each_line # 文本全部放入到字符串中
- basename = os.path.basename(filedir)
- msg1 = '文件【%s】内容如下:' %basename
- title1 = '显示文件内容'
- text = g.textbox(msg1,title1,text = string)
- if string + '\n' != text: # textbox默认结尾多一个换行符
- msg2 = '检测到文件内容发生改变,请选择以下操作:'
- title2 = '警告'
- choices = ('覆盖保存','放弃保存','另存为')
- choice = g.buttonbox(msg2,title2,choices)
- #内容变化
- if choice == '覆盖保存':
- with open(filedir,'w') as f2:
- f2.write(text)
- if choice == '另存为':
- savedir = g.filesavebox()
- if os.path.splitext(save_dir)[1] != '.txt':
- save_dir += '.txt'
- with open(savedir,'w') as f2:
- f2.write(text)
复制代码 |
|