open() in Python does not create a file if it doesn't exist

1,078,302

Solution 1

You should use open with the w+ mode:

file = open('myfile.dat', 'w+')

Solution 2

The advantage of the following approach is that the file is properly closed at the block's end, even if an exception is raised on the way. It's equivalent to try-finally, but much shorter.

with open("file.dat","a+") as f:
    f.write(...)
    ...

a+ Opens a file for both appending and reading. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing. -Python file modes

seek() method sets the file's current position.

f.seek(pos [, (0|1|2)])
pos .. position of the r/w pointer
[] .. optionally
() .. one of ->
  0 .. absolute position
  1 .. relative position to current
  2 .. relative position from end

Only "rwab+" characters are allowed; there must be exactly one of "rwa" - see Stack Overflow question Python file modes detail.

Solution 3

'''
w  write mode
r  read mode
a  append mode

w+  create file if it doesn't exist and open it in write mode
r+  open for reading and writing. Does not create file.
a+  create file if it doesn't exist and open it in append mode
'''

example:

file_name = 'my_file.txt'
f = open(file_name, 'w+')  # open file in write mode
f.write('python rules')
f.close()

[FYI am using Python version 3.6.2]

Solution 4

Good practice is to use the following:

import os

writepath = 'some/path/to/file.txt'

mode = 'a' if os.path.exists(writepath) else 'w'
with open(writepath, mode) as f:
    f.write('Hello, world!\n')

Solution 5

Change "rw" to "w+"

Or use 'a+' for appending (not erasing existing content)

Share:
1,078,302
trh178
Author by

trh178

Updated on July 06, 2021

Comments

  • trh178
    trh178 almost 3 years

    What is the best way to open a file as read/write if it exists, or if it does not, then create it and open it as read/write? From what I read, file = open('myfile.dat', 'rw') should do this, right?

    It is not working for me (Python 2.6.2) and I'm wondering if it is a version problem, or not supposed to work like that or what.

    The bottom line is, I just need a solution for the problem. I am curious about the other stuff, but all I need is a nice way to do the opening part.

    The enclosing directory was writeable by user and group, not other (I'm on a Linux system... so permissions 775 in other words), and the exact error was:

    IOError: no such file or directory.

  • SilentGhost
    SilentGhost about 14 years
    w truncates existing file. docs: Modes 'r+', 'w+' and 'a+' open the file for updating (note that 'w+' truncates the file).
  • trh178
    trh178 about 14 years
    this did the trick. thank you. i feel like an idiot now for not reading the spec. i dont think 'rw' is even acceptable there. i must have been thinking of something else.
  • Daniel Hepper
    Daniel Hepper about 14 years
    Testing for existence before opening might introduce a race condition. Probably not a big deal in this case, but something to keep in mind.
  • Nick Zalutskiy
    Nick Zalutskiy over 12 years
    Note that a+ creates a file if it does not exist and, crucially, seeks the file to the end. So if you do a read immediately after opening this way, you'll get nothing. You need to seek back to the beginning first: f.seek(0)
  • AJP
    AJP almost 12 years
  • Brian Peterson
    Brian Peterson about 11 years
    "If you need to read from a file, the file has to be exist before you open it." Thank you for saving my sanity.
  • Blacklight Shining
    Blacklight Shining over 10 years
  • Daniel F
    Daniel F about 10 years
    This is not the solution. The problem is the directory. Either the script lacks the permissions to create a file in that directory, or the directory simply doesn't exist. open('myfile.dat', 'w') is then enough.
  • The Red Pea
    The Red Pea about 10 years
    If the path is pointing at a directory, make sure that directory exists: i.e. (file = open('dir/myfile.dat', 'w+') ), make sure the dir folder, aka directory exists; Python won't create that for you
  • antibus
    antibus almost 10 years
    even worse, this code is prone to a race condition. thus, after checking if the file exists, the process could be interrupted and another process could create this file.
  • Loretta
    Loretta almost 9 years
    I try this with open(filename, 'a+') as myfile: and get IOError: [Errno 2] No such file or directory: - why it doesn't create the file?
  • Qwerty
    Qwerty almost 9 years
    @Loretta Have you checked the value of filename?
  • Loretta
    Loretta almost 9 years
    Yes, I did. It is a unicode string. I also tried with open('{}.txt'.format(filename), 'a+') as myfile:
  • Loretta
    Loretta almost 9 years
    I am not using a path. and I tried open('test.txt', 'a+') it gets following exception 'TypeError: coercing to Unicode: need string or buffer, file found' in the line if os.stat(myfile).st_size == 0:
  • Sergiy Kolodyazhnyy
    Sergiy Kolodyazhnyy almost 8 years
    You need to properly define encoding for this to work. stackoverflow.com/q/728891/3701431
  • smci
    smci almost 8 years
    This is not the solution. @TheRedPea: the OP said that the directory indeed did exist, but had 775 permissions ('o-w'), hence they couldn't write it.
  • sleblanc
    sleblanc almost 7 years
    It is bad to test a file before opening it, as it can lead to race conditions (file is deleted before it is opened). Race conditions can sometimes be used to exploit vulnerabilities in a system. "a+" mode is the best way to open the file: it creates a new file, and appends to existing files. Do not forget to wrap this in a try/except.
  • The Matt
    The Matt over 5 years
    You would also need the "w+" flag so both files will be in a read and write modes.
  • David Parks
    David Parks about 5 years
    touch does update the last modified time when used.
  • Granitosaurus
    Granitosaurus about 5 years
    @DavidParks good point, just tested it and it is indeed true on ext4 file system and python3.7.2. I don't think that's intended or desired behaviour, maybe it's a bug wtih python?
  • David Parks
    David Parks about 5 years
    Same thing when using touch at the command line in linux, so I assume it's intended behavior.
  • Jean-François Fabre
    Jean-François Fabre over 4 years
    computing mode write or append has no interest. If file doesn't exist, append mode creates it.
  • Eric Bellet
    Eric Bellet about 4 years
    Exception has occurred: FileNotFoundError... This does no work
  • chenjesu
    chenjesu over 2 years
    I have the same problem as Loretta. I try with open(self.save_to_filepath, 'a+') as f: and it gives "FileNotFoundError: [Errno 2] No such file or directory". The filepath is not in Unicode, either.
  • chenjesu
    chenjesu over 2 years
    It turns out I was using the wrong directory path- I had been running the script from 2 different current directory locations. So I used the workaround in stackoverflow.com/questions/918154/relative-paths-in-python and it worked fine.
  • ishandutta2007
    ishandutta2007 over 2 years
    @DanielF op has accepted this answer without further research. There should be option to unaccept an answer by public voting.
  • NONONONONO
    NONONONONO over 2 years
    CAREFUL about this! It will truncate the content if the file exists.
  • Chenglong Ma
    Chenglong Ma over 2 years
    Hi @NONONONONO, yes, it is what w mode does. If you want to keep the existing content, you can use a append mode. Refer to open() doc
  • Kai Petzke
    Kai Petzke over 2 years
    Mode "w+" has the disadvantage of truncating the file, if it already exists. In many cases, this is NOT what people may want.
  • Tordek
    Tordek about 2 years
    This introduces a race condition.
  • Granitosaurus
    Granitosaurus about 2 years
    @Tordek how? I don't thinks that's true.