系统: Windows 11 企业版 22H2
Python版本: Python 3.10
Python编辑器:PyCharm
class Funcs:
@staticmethod
def save():
global filename
try:
fle = open(filename, 'w')
msg = textarea.get(1.0, "end")
fle.write(msg)
fle.close()
except FileNotFoundError:
Funcs.saveas()
except TypeError:
msgbox.showinfo("提示", "您取消了保存。")
@staticmethod
def saveas():
path = file.asksaveasfilename(initialfile='未命名.txt',
defaultextension='.txt')
global filename
filename = path
fle = open(path, 'w')
msg = textarea.get(1.0, "end")
fle.write(msg)
fle.close()
Traceback (most recent call last):
File "D:\吴瀚辰\编程\程序\Python\PycharmProjects\pythonProject24\1.0.0.py", line 155, in <module>
key()
File "D:\吴瀚辰\编程\程序\Python\PycharmProjects\pythonProject24\1.0.0.py", line 110, in key
win.bind('<Control-Shift-s>', Funcs.saveas())
File "D:\吴瀚辰\编程\程序\Python\PycharmProjects\pythonProject24\1.0.0.py", line 57, in saveas
fle = open(path, 'w')
FileNotFoundError: [Errno 2] No such file or directory: ''
进程已结束,退出代码1
在Windows 10中,这种创建文件的方法是可行的,我也试过更改文件读写模式为a+, r+等,还是不起作用。
成功创建文件,无报错
看到中文先怀疑中文路径,然后看报错FileNotFoundError: [Errno 2] No such file or directory: ,print一下文件路径,看有没有这个路径和文件