Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d612de1

Browse files
author
Charles-François Natali
committed
Issue #12760: Refer to the new 'x' open mode as "exclusive creation" mode.
1 parent 138f465 commit d612de1

4 files changed

Lines changed: 13 additions & 12 deletions

File tree

Doc/library/io.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -472,12 +472,12 @@ Raw File I/O
472472
to which the resulting :class:`FileIO` object will give access.
473473

474474
The *mode* can be ``'r'``, ``'w'``, ``'x'`` or ``'a'`` for reading
475-
(default), writing, creating or appending. The file will be created if it
476-
doesn't exist when opened for writing or appending; it will be truncated
477-
when opened for writing. :exc:`FileExistsError` will be raised if it already
478-
exists when opened for creating. Opening a file for creating implies
479-
writing, so this mode behaves in a similar way to ``'w'``. Add a ``'+'`` to
480-
the mode to allow simultaneous reading and writing.
475+
(default), writing, exclusive creation or appending. The file will be
476+
created if it doesn't exist when opened for writing or appending; it will be
477+
truncated when opened for writing. :exc:`FileExistsError` will be raised if
478+
it already exists when opened for creating. Opening a file for creating
479+
implies writing, so this mode behaves in a similar way to ``'w'``. Add a
480+
``'+'`` to the mode to allow simultaneous reading and writing.
481481

482482
The :meth:`read` (when called with a positive argument), :meth:`readinto`
483483
and :meth:`write` methods on this class will only make one system call.

Doc/whatsnew/3.3.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -427,8 +427,9 @@ parameter to control parameters of the secure channel.
427427
io
428428
--
429429

430-
The :func:`~io.open` function has a new ``'x'`` mode that can be used to create
431-
a new file, and raise a :exc:`FileExistsError` if the file already exists.
430+
The :func:`~io.open` function has a new ``'x'`` mode that can be used to
431+
exclusively create a new file, and raise a :exc:`FileExistsError` if the file
432+
already exists. It is based on the C11 'x' mode to fopen().
432433

433434
(Contributed by David Townshend in :issue:`12760`)
434435

Lib/_pyio.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def open(file, mode="r", buffering=-1, encoding=None, errors=None,
4141
mode is an optional string that specifies the mode in which the file is
4242
opened. It defaults to 'r' which means open for reading in text mode. Other
4343
common values are 'w' for writing (truncating the file if it already
44-
exists), 'x' for creating and writing to a new file, and 'a' for appending
44+
exists), 'x' for exclusive creation of a new file, and 'a' for appending
4545
(which on some Unix systems, means that all writes append to the end of the
4646
file regardless of the current seek position). In text mode, if encoding is
4747
not specified the encoding used is platform dependent. (For reading and

Modules/_io/fileio.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,9 +1066,9 @@ PyDoc_STRVAR(fileio_doc,
10661066
"file(name: str[, mode: str][, opener: None]) -> file IO object\n"
10671067
"\n"
10681068
"Open a file. The mode can be 'r', 'w', 'x' or 'a' for reading (default),\n"
1069-
"writing, creating or appending. The file will be created if it doesn't\n"
1070-
"exist when opened for writing or appending; it will be truncated when\n"
1071-
"opened for writing. A `FileExistsError` will be raised if it already\n"
1069+
"writing, exclusive creation or appending. The file will be created if it\n"
1070+
"doesn't exist when opened for writing or appending; it will be truncated\n"
1071+
"when opened for writing. A `FileExistsError` will be raised if it already\n"
10721072
"exists when opened for creating. Opening a file for creating implies\n"
10731073
"writing so this mode behaves in a similar way to 'w'.Add a '+' to the mode\n"
10741074
"to allow simultaneous reading and writing. A custom opener can be used by\n"

0 commit comments

Comments
 (0)