|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- import easygui as g
- import os
- file_path = g.fileopenbox(default="*.txt")
- with open(file_path) as old_file:
- title = os.path.basename(file_path)
- msg = "文件【%s】的内容如下:" % title
- text = old_file.read()
- text_after = g.textbox(msg, title, text)
-
- if text != text_after[:-1]:
- # textbox 的返回值会追加一个换行符
- choice = g.buttonbox("检测到文件内容发生改变,请选择以下操作:", "警告", ("覆盖保存", "放弃保存", "另存为..."))
- if choice == "覆盖保存":
- with open(file_path, "w") as old_file:
- old_file.write(text_after[:-1])
- 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])
复制代码
filesavebox在设定参数default='.txt'之后,如果保存文件名后缀错误还是会报错
所以想问一下大佬们:fileopenbox和filesavebox中的default参数设定分别有什么用呢?
14.3 filesavebox()
filesavebox(msg=None, title=None, default='', filetypes=None)
filesavebox() 函数提供一个对话框,让用于选择文件需要保存的路径(带完整路径哦),如果用户选择 “Cancel” 则返回 None。
default 参数应该包含一个文件名(例如当前需要保存的文件名),当然也可以设置为空的,或者包含一个文件格式掩码的通配符。
filetypes 参数的设置方法请参考 fileopenbox() 函数。
保存文件那个default是 设置默认文件名,就像新建文本文件一样
|
|