为什么无法保存文字到电脑
源码:import easygui as eg
import os
while True:
choice = eg.buttonbox('欢迎进入光电记事本,请选择新建或打开','光电记事本',['新建','打开'])
if choice == '打开':
fileName = eg.fileopenbox(default = '*.txt')
temp = open(fileName,'r')
temp1 = ''
for each_line in temp:
temp1 = temp1 + each_line
temp.close
newWrite = eg.textbox(text = temp1,title = '光电记事本')
temp = open(fileName,'w')
temp.write(newWrite)
temp.close
else:
newWrite = eg.textbox(text = '',title = '光电记事本')
while True:
path = eg.enterbox('请输入文件保存的路径+文件名(不带后缀)','光电记事本')
try:
temp = open(path+'.txt','w')
temp.write(newWrite)
temp.close
break
except OSError:
eg.msgbox('文件路径输入格式错误!示例:D:\\示例\\示例文件(示例文件.txt即保存在D盘的示例文件夹)','光电记事本')
请输入原因和改好的代码!谢谢!
本帖最后由 isdkz 于 2022-3-27 20:01 编辑
close() 都少了括号import easygui as eg
import os
while True:
choice = eg.buttonbox('欢迎进入光电记事本,请选择新建或打开','光电记事本',['新建','打开'])
if choice == '打开':
fileName = eg.fileopenbox(default = '*.txt')
temp = open(fileName,'r')
temp1 = ''
for each_line in temp:
temp1 = temp1 + each_line
temp.close() # 这里少了括号
newWrite = eg.textbox(text = temp1,title = '光电记事本')
temp = open(fileName,'w')
temp.write(newWrite)
temp.close() # 这里少了括号
else:
newWrite = eg.textbox(text = '',title = '光电记事本')
while True:
path = eg.enterbox('请输入文件保存的路径+文件名(不带后缀)','光电记事本')
try:
temp = open(path+'.txt','w')
temp.write(newWrite)
temp.close() # 这里少了括号
break
except OSError:
eg.msgbox('文件路径输入格式错误!示例:D:\\示例\\示例文件(示例文件.txt即保存在D盘的示例文件夹)','光电记事本')
|