首页 > 代码库 > 打开文件修改并存储

打开文件修改并存储

 1 import easygui as g
 2 import os 
 3 
 4 
 5 file_path = g.fileopenbox(default=F:\\)
 6 with open(file_path) as f:
 7     title = 内容显示
 8     file_name = os.path.basename(file_path)
 9     msg = 文件【%s】的内容显示如下% file_name
10     
11     text = f.read()
12 
13     new_text = g.textbox(msg,title,text)
14 
15 
16     if new_text[:-1] != text:
17         msg=检测到文件内容发生改变请选择以下操作:
18         choices = (覆盖保存,放弃保存,另存为)
19         title = 警告
20         choice = g.buttonbox(msg, title, choices)
21         if choice == 覆盖保存:
22             with open(file_path,w) as f:
23                 f.write(new_text[:-1])
24         if choice == 另存为:
25             
26             new_path = g.filesavebox(default=*.txt)
27             with open(new_path,w) as f:
28                 f.write(new_text[:-1])
29             
30         if choice == 放弃保存:
31             pass
32     

 

打开文件修改并存储