19

I'm having a small problem: while using the function open() with the 'w' mode, all documentation says that the file is created if it does not exist. Unfortunately, it appears that in my case, I get a FileNotFound error for some reason.

with open(newFileName, 'w') as newFile:
    #CODE

I receive the following error:

FileNotFoundError: [Errno 2] No such file or directory: 'path of file I have specified'

Any idea of why this might be? Thanks in advance!

EDIT: For those asking if the directory exists or not, I have made small changes to the code that might show you it is indeed the good path.

if not os.path.exists("example"):
    os.makedirs("example")

BASE_DIR = os.path.dirname(os.path.abspath(__file__))

newFileName = "example/restOfPath"
newFileName = os.path.join(BASE_DIR,newFileName)

with open(newFileName, 'w') as newFile:

I still get the following error:

FileNotFoundError: [Errno 2] No such file or directory: 'correctPathToDirectory/example/restOfPath'

EDIT2: Disregard this question, problem solved. A second directory was being created after "example", thus it not working. Silly error.

Balaam
  • 331
  • 1
  • 2
  • 8
  • could you do a `print(newFileName)` and see the file mentioned – Praveen Aug 15 '15 at 11:58
  • 3
    Can you give us the value of newFileName? I suspect it may be e.g. /some/dir/filename, where /some/dir doesn't exist. – Bastien Léonard Aug 15 '15 at 11:59
  • @WédneyYuri The OP is not asking to open a file in read and write mode... – nbro Aug 15 '15 at 12:01
  • I did think of that possibility so I created the directory right before that line of code. – Balaam Aug 15 '15 at 12:02
  • http://stackoverflow.com/questions/25924720/filenotfounderror-errno-2, http://stackoverflow.com/questions/22282760/filenotfounderror-errno-2-no-such-file-or-directory, http://stackoverflow.com/questions/28198915/filenotfounderror-errno-2-no-such-file-or-directory-classa-in-python-alth – nbro Aug 15 '15 at 12:03
  • @Balaam: You _should_ get an error message if your code couldn't successfully create the new directory, but did you check that it was created ok? – PM 2Ring Aug 15 '15 at 12:20
  • Yes, the directory "example" is created. – Balaam Aug 15 '15 at 12:22
  • @Balaam: What are the permissions on the directory "example"? – PM 2Ring Aug 15 '15 at 12:25
  • It gives the correct path. Disregard this question I have already fixed the problem. It seems my code was creating another directory AFTER "example" and this was the problem. Completely silly, I know. Thanks for all your help! – Balaam Aug 15 '15 at 12:26

1 Answers1

29

The cause for this error might be that the directory containing your new file does not yet exist.

open() with 'w' only creates the non-existing file for you but not the whole directory path. So you first need to create the directories for the file.

ndmeiri
  • 4,979
  • 12
  • 37
  • 45
x squared
  • 3,173
  • 1
  • 26
  • 41
  • I did think of that possibility so I created the directory right before that line of code. – Balaam Aug 15 '15 at 12:03
  • 9
    Then another possibility is that your program has no write rights in the specified directory. – x squared Aug 15 '15 at 12:11
  • Maybe, @x-squared , but this code will run on other people's machines, I can't ask them to chmod before running the code. Is there a way to circumvent this? – Balaam Aug 15 '15 at 12:14
  • Is it a directory of your choice? You can always write in the user local directories, e.g. $APPDATA in windows and $HOME/.config in Linux. – x squared Aug 15 '15 at 12:18
  • I have edited the post with more information. – Balaam Aug 15 '15 at 12:20
  • There we have the problem. In case "blocks" does not exist, you create folder "example". But even if "blocks" exists, you change the file path to "example/...". – x squared Aug 15 '15 at 12:25
  • In case others like me come here looking for the code to add directory if it does not exists. Here is the link: https://stackoverflow.com/questions/600268/mkdir-p-functionality-in-python – Stian Nov 16 '20 at 09:36