What does the new open file mode "x" do in python 3?
here is the doc of python 3:
'r': open for reading (default)
'w': open for writing, truncating the file first
'x': open for exclusive creation, failing if the file already exists
'a': open for writing, appending to the end of the file if it exists
'b': binary mode
't': text mode (default)
'+': open a disk file for updating (reading and writing)
'U': universal newlines mode (deprecated)
What does "exclusive creation" mean?
I test the "x" mode and find some:
- It can not be used with "r/w/a"
- "x" is only writeable. "x+" can write and read
- The file must not exist before
open - The file will be created after
open
So, "x" is similar to "w". But for "x", if the file exists, raise FileExistsError. For "w", it will simply create a new file / truncate the existed file.
Am I right? Is this the only difference?
xmeans: The same aswbut raises an exception if the file already exists. - Martijn Pieters♦