tags:

views:

591

answers:

3

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'

Note that the "dir/dir2/dir3/" does exist! Also the "w" mode should create the file if it doesn't exist, right?

Cheers,

Ivar

A: 

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.

Felix
+3  A: 

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

ChristopheD
+2  A: 

Ignoring the security considerations (for now) - are you sure the 'current directory' has the sub dir/dir2/dir3 under it. 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")
Lee
My god i feel stupid, i checked the dirs like 16 times! But i made a unnoticable typo in one of the dirs.... I'm gonna go cry in a corner now, thnx for the help!
lugte098