Reputation: 43
In the docs of the open()
built-in function, the meaning of "+" is as follows:
open a disk file for updating (reading and writing)
But when I use open()
to create a new file with python 3.5 in windows 7, I got a FileNotFoundError
.
tmp_file=open(str(temp_path),'r+')
From the explanation of open()
in the docs, wouldn't it create a new empty file if the file specified doesn't exist when using the r+
mode?
Upvotes: 4
Views: 3065
Reputation: 416
You can see the full documentation of the built-in open function here. So the + sign adds update functionality to an added parameter. It does not give you permission to create a new file. That is why you are getting the error.
Upvotes: 1
Reputation: 62536
r+
mode will open an existing file for write, but will not create the file if it doesn't exists.
You should open the file with w
if you want to create a new file.
Upvotes: 0