为什么我突然不能再用 Python 写入文件了?

问题描述

每当我尝试以任何方式写入或修改文件的数据时,我每次都会收到此错误

OSError: [Errno 9] Bad file descriptor

这是我一直在尝试做的:

# output.txt is the file already created inside of the same directory

with open(__file__.rsplit("\\",1)[0] + "\\output.txt","w") as f:
    f.write("this should write to 'output.txt'")

我会尝试使用 json 转储或附加普通数据,但我不断收到相同的错误

在上面的例子中,这是终端执行后的整个输出

C:\Users\USER\Documents\Programming\Code\Python\Testing>c:\Users\USER\Documents\Programming\Code\Python\Testing\z_3.py
OSError: [Errno 9] Bad file descriptor

During handling of the above exception,another exception occurred:

Traceback (most recent call last):
  File "C:\Users\USER\Documents\Programming\Code\Python\Testing\z_3.py",line 4,in <module>
    f.write("this should write to 'output.txt'")
OSError: [Errno 9] Bad file descriptor

这一切都很奇怪,因为我已经能够正常写入文件,但我最近刚刚重新安装了 Windows,现在它无法工作。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)