| 
 | 
 
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册  
 
x
 
import easygui as g 
import 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)[1] != '.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后缀 
 
 
 |   
 
 
 
 |