Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to use open(filename, 'w' ) gives IOError: [Errno 2] No such file or directory if directory doesn't exist

I am trying to create and write to a text file using Python. I have searched and cannot find a solution/reason for this error.

Here's the code that doesn't work:

afile = 'D:\\temp\\test.txt' outFile = open(afile, 'w' ) outFile.write('Test.') outFile.close()  # Error: 2 # Traceback (most recent call last): #   File "<maya console>", line 1, in <module> # IOError: [Errno 2] No such file or directory: 'D:\\temp\\test.txt' # 

Most answers I found related to the slashes in the path, so...

I tried 'D:/temp/test.txt' and got an error. I tried r'D:\temp\test.txt' and got an error. 

When I try to create a file at the root of D:/ I have success.

'D:/test.txt' works. 'D:\\test.txt' works. r'D:\test.txt' works. 

It seems that I can't create the directory path I would like while trying to create the file. What is the correct method for creating files at a specific path with Python on Windows(7)? Am I misunderstanding what open() can do? Does it create directories if they don't exist or do I need to explicitly create the directory path before I use open() in 'write' mode to create a file?

like image 391
gonzalimator Avatar asked Sep 12 '13 08:09

gonzalimator


People also ask

How do I fix Errno 2 No such file or directory?

The error "FileNotFoundError: [Errno 2] No such file or directory" is telling you that there is no file of that name in the working directory. So, try using the exact, or absolute path. In the above code, all of the information needed to locate the file is contained in the path string - absolute path.

Can't open file Errno 2 No such file or directory?

The Python "FileNotFoundError: [Errno 2] No such file or directory" occurs when we try to open a file that doesn't exist in the specified location. To solve the error, move the file to the directory where the Python script is located if using a local path, or use an absolute path.

How do I fix Python error No such file or directory?

To solve No Such File Or Directory Error in Python, ensure that the file exists in your provided path. To check all the files in the directory, use the os. listdir() method.

Why am I getting a No such file or directory?

log No such file or directory” the problem is most likely on the client side. In most cases, this simply indicates that the file or folder specified was a top-level item selected in the backup schedule and it did not exist at the time the backup ran.


2 Answers

You are correct in surmising that the parent directory for the file must exist in order for open to succeed. The simple way to deal with this is to make a call to os.makedirs.

From the documentation:

os.makedirs(path[, mode])

Recursive directory creation function. Like mkdir(), but makes all intermediate-level directories needed to contain the leaf directory.

So your code might run something like this:

filename = ... dirname = os.path.dirname(filename) if not os.path.exists(dirname):     os.makedirs(dirname) with open(filename, 'w'):     ... 
like image 80
David Heffernan Avatar answered Sep 20 '22 06:09

David Heffernan


If you try to create a file in a directory that doesn't exist, you will get that error.

You need to ensure the directory exists first. You can do that with os.makedirs() as per this answer.

like image 24
paxdiablo Avatar answered Sep 21 '22 06:09

paxdiablo