简体   繁体   中英

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

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?

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

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.

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

我有同样的错误,但在我的情况下,原因是在 Windows 下,路径长度超过 ~250 个字符。

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

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM