Kesong Xie Kesong Xie - 20 days ago 12
Python Question

failed to create new file in pycharm using open

I tried to create a empty file using

open
in pycharm, and it failed to create one without complaining anything

In Pycharm

fw = open("sample.txt", "w")
fw.write("what's up")
fw.close()


I used the same code in terminal, and it successfully created the sample.txt file.

Do I have to do some configuration in Pycharm in order to make it work?

Answer

I guess it is because you are using a relative path in fw = open("sample.txt", "w")

Try putting an absolute path like fw = open("/path/to/sample.txt", "w").

I am saying "I guess" because the error description and the stack trace are the clues to find the problem, so you should check them and always provide them when asking for help.