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

Skip to content

Commit b93f9fa

Browse files
author
Charles-François Natali
committed
Issue #12760: Add some mising documentation about the new x exclusive
creation flag to open().
1 parent 185f401 commit b93f9fa

1 file changed

Lines changed: 10 additions & 5 deletions

File tree

Doc/library/functions.rst

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -797,17 +797,19 @@ are always available. They are listed here in alphabetical order.
797797
*mode* is an optional string that specifies the mode in which the file is
798798
opened. It defaults to ``'r'`` which means open for reading in text mode.
799799
Other common values are ``'w'`` for writing (truncating the file if it
800-
already exists), and ``'a'`` for appending (which on *some* Unix systems,
801-
means that *all* writes append to the end of the file regardless of the
802-
current seek position). In text mode, if *encoding* is not specified the
803-
encoding used is platform dependent. (For reading and writing raw bytes use
804-
binary mode and leave *encoding* unspecified.) The available modes are:
800+
already exists), ``'x'`` for exclusive creation and ``'a'`` for appending
801+
(which on *some* Unix systems, means that *all* writes append to the end of
802+
the file regardless of the current seek position). In text mode, if
803+
*encoding* is not specified the encoding used is platform dependent. (For
804+
reading and writing raw bytes use binary mode and leave *encoding*
805+
unspecified.) The available modes are:
805806

806807
========= ===============================================================
807808
Character Meaning
808809
--------- ---------------------------------------------------------------
809810
``'r'`` open for reading (default)
810811
``'w'`` open for writing, truncating the file first
812+
``'x'`` open for exclusive creation, failing if the file already exists
811813
``'a'`` open for writing, appending to the end of the file if it exists
812814
``'b'`` binary mode
813815
``'t'`` text mode (default)
@@ -898,6 +900,7 @@ are always available. They are listed here in alphabetical order.
898900

899901
.. versionchanged:: 3.3
900902
The *opener* parameter was added.
903+
The ``'x'`` mode was added.
901904

902905
The type of file object returned by the :func:`open` function depends on the
903906
mode. When :func:`open` is used to open a file in a text mode (``'w'``,
@@ -926,6 +929,8 @@ are always available. They are listed here in alphabetical order.
926929

927930
.. versionchanged:: 3.3
928931
:exc:`IOError` used to be raised, it is now an alias of :exc:`OSError`.
932+
:exc:`FileExistsError` is now raised if the file opened in exclusive
933+
creation mode (``'x'``) already exists.
929934

930935

931936
.. XXX works for bytes too, but should it?

0 commit comments

Comments
 (0)