|
|
发表于 2014-9-8 10:15:34
|
显示全部楼层
本帖最后由 wei_Y 于 2014-9-8 10:35 编辑
- if choice == '覆盖保存':
- with open(filePath, 'w') as oldFile:
- oldFile.write(textAfter[:-1])
- if choice == '放弃保存': #这一个if在上一个if底下了,不过因为里面是pass,删了之后完全不影响功能。
- pass
- if choice == '另存为...': #这行就不行了。因为有要执行的功能,你点另存为他找不到要执行什么,所以pass。改法是你把缩进和'覆盖保存的if在同一行。'
复制代码- import easygui as g
- import os
- filePath = g.fileopenbox(default = '*.txt')
- with open(filePath) as oldFile:
- title = os.path.basename(filePath)
- msg = '文件【%s】的内容如下:' % title
- text = oldFile.read()
- textAfter = g.textbox(msg, title, text)
- if text != textAfter[:-1]:
- #textbox 的返回值会追加一个换行符
- choice = g.buttonbox("检测到文件内容发生改变,请选择以下操作:", "警告", ("覆盖保存", "放弃保存", "另存为..."))
- if choice == '覆盖保存':
- with open(filePath, 'w') as oldFile:
- oldFile.write(textAfter[:-1])
- if choice == '放弃保存':
- pass
- if choice == '另存为...': #以上两个if正常运行,但当选择'另存为...',就终止运行,没有弹出预期的filesavebox, 何解??
- anotherPath = g.filesavebox(default = '.txt')
- if os.path.splitext(anotherPath)[1] != '.txt':
- #分离文件名与扩展名,返回(f_name,f_extension)元组
- anotherPath += '.txt'
- with open(anotherPath, 'w') as newFile:
- newFile.write(textAfter[:-1])
复制代码
|
|