@@ -38,21 +38,22 @@ def open(file, mode="r", buffering=-1, encoding=None, errors=None,
3838 wrapped. (If a file descriptor is given, it is closed when the
3939 returned I/O object is closed, unless closefd is set to False.)
4040
41- mode is an optional string that specifies the mode in which the file
42- is opened. It defaults to 'r' which means open for reading in text
43- mode. Other common values are 'w' for writing (truncating the file if
44- it already exists), and 'a ' for appending (which on some Unix systems,
45- means that all writes append to the end of the file regardless of the
46- current seek position). In text mode, if encoding is not specified the
47- encoding used is platform dependent. (For reading and writing raw
48- bytes use binary mode and leave encoding unspecified.) The available
49- modes are:
41+ mode is an optional string that specifies the mode in which the file is
42+ opened. It defaults to 'r' which means open for reading in text mode. Other
43+ 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
45+ (which on some Unix systems, means that all writes append to the end of the
46+ file regardless of the current seek position). In text mode, if encoding is
47+ not specified the encoding used is platform dependent. (For reading and
48+ writing raw bytes use binary mode and leave encoding unspecified.) The
49+ available modes are:
5050
5151 ========= ===============================================================
5252 Character Meaning
5353 --------- ---------------------------------------------------------------
5454 'r' open for reading (default)
5555 'w' open for writing, truncating the file first
56+ 'x' create a new file and open it for writing
5657 'a' open for writing, appending to the end of the file if it exists
5758 'b' binary mode
5859 't' text mode (default)
@@ -63,7 +64,8 @@ def open(file, mode="r", buffering=-1, encoding=None, errors=None,
6364
6465 The default mode is 'rt' (open for reading text). For binary random
6566 access, the mode 'w+b' opens and truncates the file to 0 bytes, while
66- 'r+b' opens the file without truncation.
67+ 'r+b' opens the file without truncation. The 'x' mode implies 'w' and
68+ raises an `FileExistsError` if the file already exists.
6769
6870 Python distinguishes between files opened in binary and text modes,
6971 even when the underlying operating system doesn't. Files opened in
@@ -154,23 +156,24 @@ def open(file, mode="r", buffering=-1, encoding=None, errors=None,
154156 if errors is not None and not isinstance (errors , str ):
155157 raise TypeError ("invalid errors: %r" % errors )
156158 modes = set (mode )
157- if modes - set ("arwb +tU" ) or len (mode ) > len (modes ):
159+ if modes - set ("axrwb +tU" ) or len (mode ) > len (modes ):
158160 raise ValueError ("invalid mode: %r" % mode )
161+ creating = "x" in modes
159162 reading = "r" in modes
160163 writing = "w" in modes
161164 appending = "a" in modes
162165 updating = "+" in modes
163166 text = "t" in modes
164167 binary = "b" in modes
165168 if "U" in modes :
166- if writing or appending :
169+ if creating or writing or appending :
167170 raise ValueError ("can't use U and writing mode at once" )
168171 reading = True
169172 if text and binary :
170173 raise ValueError ("can't have text and binary mode at once" )
171- if reading + writing + appending > 1 :
174+ if creating + reading + writing + appending > 1 :
172175 raise ValueError ("can't have read/write/append mode at once" )
173- if not (reading or writing or appending ):
176+ if not (creating or reading or writing or appending ):
174177 raise ValueError ("must have exactly one of read/write/append mode" )
175178 if binary and encoding is not None :
176179 raise ValueError ("binary mode doesn't take an encoding argument" )
@@ -179,6 +182,7 @@ def open(file, mode="r", buffering=-1, encoding=None, errors=None,
179182 if binary and newline is not None :
180183 raise ValueError ("binary mode doesn't take a newline argument" )
181184 raw = FileIO (file ,
185+ (creating and "x" or "" ) +
182186 (reading and "r" or "" ) +
183187 (writing and "w" or "" ) +
184188 (appending and "a" or "" ) +
@@ -205,7 +209,7 @@ def open(file, mode="r", buffering=-1, encoding=None, errors=None,
205209 raise ValueError ("can't have unbuffered text I/O" )
206210 if updating :
207211 buffer = BufferedRandom (raw , buffering )
208- elif writing or appending :
212+ elif creating or writing or appending :
209213 buffer = BufferedWriter (raw , buffering )
210214 elif reading :
211215 buffer = BufferedReader (raw , buffering )
0 commit comments