open file in "w" mode: IOError: [Errno 2] No such file or directory

Question:

When I try to open a file in write mode with the following code:

packetFile = open("%s/%s/%s/%s.mol2" % ("dir", "dir2", "dir3", "some_file"), "w")

I get the following error:

IOError: [Errno 2] No such file or directory: 'dir/dir2/dir3/some_file.mol2'

The w mode should create the file if it doesn’t exist, right? So how can this error ever occur?

Asked By: lugte098

||

Answers:

Check that that the script has write permissions on that directory. Try this:

chmod a+w dir/dir2/dir3

Note that this will give write permissions to everyone on that directory.

Answered By: Felix

Since you don’t have a ‘starting’ slash, your python script is looking for this file relative to the current working directory (and not to the root of the filesystem). Also note that the directories leading up to the file must exist!

And: use os.path.join to combine elements of a path.

e.g.: os.path.join("dir", "dir2", "dir3", "myfile.ext")

Answered By: ChristopheD

You’ll see this error if the directory containing the file you’re trying to open does not exist, even when trying to open the file in w mode.

Since you’re opening the file with a relative path, it’s possible that you’re confused about exactly what that directory is. Try putting a quick print to check:

import os

curpath = os.path.abspath(os.curdir)
packet_file = "%s/%s/%s/%s.mol2" % ("dir", "dir2", "dir3", "some_file")
print "Current path is: %s" % (curpath)
print "Trying to open: %s" % (os.path.join(curpath, packet_file))

packetFile = open(packet_file, "w")
Answered By: Lee

I had the same error, but in my case the cause was, under Windows, a path longer than ~250 characters.

Answered By: Antonio

Similar issue happened in windows environment. Solution was to add “C:” to absolute path.
My goal was to save some files in user Desktop

file_path = os.path.join(os.environ["HOMEPATH"], os.path.join("Desktop", 
    "log_file.log_%s_%s" %(
    strftime("%Y_%m_%d", localtime()), "number_1")))

then I was trying to open this directory to save
such as

file_ref = open(file_path, "w")

I added this in order to run

file_ref = open(("C:\"+file_path), "w")
Answered By: MSK

This error will also occur if you are trying to overwrite a broken soft link to a file with the same name. In that case, delete the broken soft link and you will be able to write the new file.

Answered By: abruin

I had the same issue, but my root cause was different than anyone’s here. Thought I’d share in case someone else ran into the same issue.

In my case, I had accidentally misplaced my parentheses on the ‘with’ line:

with (open(os.path.join(curpath, unique_name)), 'w') as fw:

Gave the following error (modified to obscure company details and for clarity):

Traceback (most recent call last):
  File "./crap.py", line 60, in uniquify
    with (open(os.path.join(curpath, unique_name)), 'w') as fw:
IOError: [Errno 2] No such file or directory: '/<mypath>/bin/python/<filename>'

These parentheses put the ‘w’ with the with() function, not with open() as intended. I’m surprised it gives this IO Error, which implies it’s something wrong with the open() call, which made this much harder to track down than if it obviously came from the with() call.

I didn’t believe these results but just modified it again to replicate, and yes, I get the same error.

When I switch parentheses to the correct version:

with (open(os.path.join(curpath, unique_name), 'w')) as fw:

it works as intended.

Answered By: Joel Wigton
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.