105

If I try to open a file that doesn't exist yet it will create it on the same directory from where the program is being executed. The problem comes that when I try to open it, I get this error:

IOError: [Errno 2] No such file or directory:
'C:\Users\myusername\PycharmProjects\Tests\copy.txt'.

I even tried specifying a path as you can see in the error.

import os

THIS_FOLDER = os.path.dirname(os.path.abspath(__file__))
my_file = os.path.join(THIS_FOLDER, 'copy.txt')
7
  • 2
    We cannot tell you what error you have in your code if you don't show us your code. Commented Feb 24, 2018 at 3:38
  • 1
    @JustHalf This code doesn't raise an error when I run it in PyCharm? Commented Feb 24, 2018 at 4:09
  • 4
    That code doesn't open a file, it just creates a path object. Commented Feb 24, 2018 at 4:54
  • 1
    The IDE you're using is irrelevant here. It does not affect the behavior of Python code that you run from it. Commented Feb 24, 2018 at 9:24
  • This code works on my computer if I substitute a filename for __file__. It produces a string. '/Users/myusername/copy.txt' in my case Commented Feb 24, 2018 at 16:28

3 Answers 3

212

Looks like you forgot the mode parameter when calling open, try w:

with open("copy.txt", "w") as file:
    file.write("Your text goes here")

The default value is r and will fail if the file does not exist

'r' open for reading (default)
'w' open for writing, truncating the file first

Other interesting options are

'x' open for exclusive creation, failing if the file already exists
'a' open for writing, appending to the end of the file if it exists

See Doc for Python 2.7 or Python 3.6

Sign up to request clarification or add additional context in comments.

Comments

19
# Method 1
f = open("Path/To/Your/File.txt", "w")   # 'r' for reading and 'w' for writing
f.write("Hello World from " + f.name)    # Write inside file 
f.close()                                # Close file 

# Method 2
with open("Path/To/Your/File.txt", "w") as f:   # Opens file and casts as f 
    f.write("Hello World form " + f.name)       # Writing
    # File closed automatically

There are many more methods but these two are most common. Hope this helped!

Comments

1

I'd like to give not an answer, but a general tip on creating files with Python in the home dir:

Do not use '~/myfile' to create "myfile" in the home dir. Use expanded '/home/user/myfile' or Python will give you a similar error. I came to this post exactly because of this problem.

3 Comments

have similar unexpected behavior. Python 3.11 on macos 14.3.1. open( "myfile", "w") fails with error file not found, unless file path is either only file name or is absolute file path. Any other relative file path causes error FileNotFoundError: [Errno 2] No such file or directory: ...
I think in your case the correct was to use './myfile'
Thanks. My problem is resolved. I didn't realize when running PyCharm debugger, it makes CWD the script dir! I thought project dir was CWD. My trust in Python is restored!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.