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

PythonFile Io

Python Problem Overview


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

Gives me 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?

Python Solutions


Solution 1 - Python

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

Solution 2 - Python

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

Solution 3 - Python

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

Solution 4 - Python

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

Solution 5 - Python

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.

Solution 6 - Python

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.

Solution 7 - Python

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionlugte098View Question on Stackoverflow
Solution 1 - PythonLeeView Answer on Stackoverflow
Solution 2 - PythonChristopheDView Answer on Stackoverflow
Solution 3 - PythonAntonioView Answer on Stackoverflow
Solution 4 - PythonMSKView Answer on Stackoverflow
Solution 5 - PythonJoel WigtonView Answer on Stackoverflow
Solution 6 - PythonFelixView Answer on Stackoverflow
Solution 7 - PythonabruinView Answer on Stackoverflow