Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
239 views
in Technique[技术] by (71.8m points)

windows - Why can I suddenly no longer write to files in Python?

Whenever I try to write or modify the data of a file in any way I get this error everytime:

OSError: [Errno 9] Bad file descriptor

Here's what I've been trying to do:

# 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'")

I would try to dump with json or append with normal data but I'm continuously recieving the same error.

In the above example, here is the entire output of the terminal after execution:

C:UsersUSERDocumentsProgrammingCodePythonTesting>c:UsersUSERDocumentsProgrammingCodePythonTestingz_3.py
OSError: [Errno 9] Bad file descriptor

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:UsersUSERDocumentsProgrammingCodePythonTestingz_3.py", line 4, in <module>
    f.write("this should write to 'output.txt'")
OSError: [Errno 9] Bad file descriptor

This is all very strange because I've been able to write to files normally but I've just recently reinstalled windows and now it won't work.

question from:https://stackoverflow.com/questions/65931766/why-can-i-suddenly-no-longer-write-to-files-in-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...