As @Martjin already said, you already answered your question. I would only reinforce the explanation in the manual to better understand the text.
'x': open for exclusive creation if file already exists
When you specify exclusive creation , this clearly means that you should use this mode to exclusively create the file. The need for this is necessary if you do not accidentally truncate / add an existing file with any of the w or a modes.
In the absence of this, developers should be careful to check for the presence of a file before jumping to open the file for updating.
In this mode, your code will simply be written as
try: with open("fname", "x") as fout:
Previously, although your code could have been written as
import os.path if os.path.isfile(fname):
Abhijit
source share