Read and write calls will fail at runtime if open
is called with incorrect modes
.
The second argument of open
specifies the "mode" in which the file is opened. For example opening in write mode:
If unspecified, mode defaults to 'rt'
which allows calling read
, readline
and readlines
. 't'
means the contents will be of type str
.
Python will throw an exception at runtime if a file read/write method is used without the file being opened in a mode that allows it. File modes are a source of bugs that will happen at runtime because there is no in-language feature preventing a developer from specifying modes that are invalid with the way the file will later be interacted with. If the invalid modes are not hit during testing then they will ultimately fail in prod.
The full options are:
Meaning | Character |
---|---|
Open for reading (default) | r |
Encode as text (default) | t |
Open for writing, truncating it first | w |
Open and raise FileExistsError if file already exists | x |
Open for writing, appending to the end of file if it exists | a |
Open for updating (reading and writing) | + |
Encode a bytes | b |
For backwards compatibility. Has no effect in Python 3 | U |
In Python 2.7 'U'
simplified the handling of newlines across platforms, so a file's new lines work on Unix even if created Windows. This is the default behaviour in Python 3, so 'U' is not needed in Python 3.
Python supports various methods of reading files: read
, readline
and readlines
. To use the read methods 'r'
mode must be in use (or must be in read/write mode):
Python will throw an exception if read
, readline
and readlines
are used without the file being opened with 'r'
mode:
Python supports write
for writing to files. To use them 'w'
, or 'a'
, or 'x'
mode must be in use (or read/write mode):
Python will throw an exception if write
is used without the file being opened with 'w'
, or 'a'
, or 'x'
mode (or read/write mode):
Python will also throw an exception if exclusive mode is used but the file already exists
Exclusive mode is useful for creating thread-safe pid files:
Python allows the file to be opened in both read and write mode by using '+'
alongside 'r'
, 'w'
, or 'a'
, so there is no need to open the same file multiple times in different modes.
Note though that 'r+'
, 'a+'
, and 'w+'
do very different things:
'r+'
opens an existing file for reading and writing without truncating it. The file pointer is at the start of the file, so read
will return the current contents of the file, but write
will override the current contents unless the file pointer is be moved to the end of the fie via f.seek(...)
'a+'
opens a file for reading and writing without truncating it. The file pointer is at the end of the file, so read
will return an ''
(even if there is contents in the file), and write
will append the end of the existing file contents. The file pointer can be moved to the start of the fie via f.seek(0)
'w+'
creates a new file or truncates an existing file, then opens it for reading and writing.That means there is no need to do the following:
If our GitHub code review bot spots this issue in your pull request it gives this advice:
1 | + | with open('some/path.txt') as f: |
Read and write calls will fail at runtime if open
is called with incorrect modes
.
- | with open('some/path.txt') as f: |
+ | with open('some/path.txt', 'w') as f: |
2 | + | content = f.write('foo') |