微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决为什么我突然不能再用 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 举报,一经查实,本站将立刻删除。