这样试试:
# 提供一个文件夹浏览框,让用户选择需要打开的文本文件,打开并显示文件内容
import easygui as g
# 找到相应的文件并显示文件路径
me = g.fileopenbox(msg='文件【record】的内容如下', title='显示文件内容', default='*', filetypes=None, multiple=False)
file = open(me, 'r+')
file_new = g.textbox(msg='文件【record】的内容如下', title='显示文件内容', text=file, codebox=False,
callback=None, run=True)
if file_new != file: # 文件可以直接相比
choice = g.buttonbox(msg='', title='文件已经被修改,请选择以下操作: ', choices=('覆盖保存', '放弃保存', '另存为..'), image=None, images=None,
cancel_choice=None, callback=None, run=True)
if choice == '覆盖保存':
code = []
for each_line in file_new:
code.append(each_line)
file.close()
file_new_1 = open(file.name, 'w')
file_new_1.writelines(code)
file_new_1.close()
if choice == '放弃保存':
file.close()
if choice == '另存为..':
title = g.enterbox(msg='', title='请输入新的文件名: ', default='', strip=True, image=None, root=None)
final_tltle_foddle = title + '.txt'
code = []
for each_line in file_new:
code.append(each_line)
final_file = open(final_tltle_foddle, 'w+')
final_file.writelines(code)
final_file.close()
|