3

In the docs of open() build in function of python,the meaning of "+" is as follows:

open a disk file for updating(reading and writing)

but when i use the open() build-in to create a new file with python 3.5 in win7,i got the "FileNotFoundError".

tmp_file=open(str(temp_path),'r+')

as the explanation of open() in doc,should't it create a new empty file if the file specifiled is not exist?when use the "r+" mode?

Master Lee
  • 33
  • 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

2 Answers2

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
  • 57,326
  • 8
  • 92
  • 123
0

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

D4Vinci
  • 3
  • 7