4

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?

duckboycool
  • 2,425
  • 2
  • 8
  • 23
Master Lee
  • 43
  • 3
  • take a look at http://stackoverflow.com/questions/1466000/python-open-built-in-function-difference-between-modes-a-a-w-w-and-r – Fujiao Liu Aug 17 '16 at 03:04
  • thanks a lot.very clear explanation.just understood: if the doc didin't say it will create, it just won't create, even if it's open for writing – Master Lee Aug 17 '16 at 03:21

3 Answers3

1

You can see the full documentation of the built-in open function here. Short Description of the parameters 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.

Hasidul Islam
  • 324
  • 3
  • 12
0

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.

Dekel
  • 60,707
  • 10
  • 101
  • 129
0

You should use :
file = open(str(temp_path), 'w+')

D4Vinci
  • 53
  • 9