35讲的第3题
import easygui as gimport os
def open_file(file_name):
f = open(file_name)
msg = '文件[%s]的内容如下:' % file_name
title = str(file_name)
text = f.read()
text_after = g.textbox(msg,title,text)
if text != text_after:
choice = g.buttonbox("检测到文件内容发生改变,请选择以下操作:", "警告", ("覆盖保存", "放弃保存", "另存为..."))
if choice == '覆盖保存':
f = open(file_name,'w')
f.write(text_after)
if choice == "放弃保存":
pass
if choice == "另存为...":
another_path = g.filesavebox(default=".txt")
if os.path.splitext(another_path) != '.txt':
another_path += '.txt'
with open(another_path, "w") as new_file:
new_file.write(text_after[:-1])
file_name = g.fileopenbox(default='*.txt')
open_file(file_name)
代码中的another_path += '.txt' 不明白,请大神帮忙解答一下,
如果从filesavebox获取的文件名称不是.txt结尾的。强制加上.txt后缀 谢谢大神!!
页:
[1]