File modes
suggest changeThere are different modes you can open a file with, specified by the mode
parameter. These include:
'r'
- reading mode. The default. It allows you only to read the file, not to modify it. When using this mode the file must exist.'w'
- writing mode. It will create a new file if it does not exist, otherwise will erase the file and allow you to write to it.'a'
- append mode. It will write data to the end of the file. It does not erase the file, and the file must exist for this mode.'rb'
- reading mode in binary. This is similar tor
except that the reading is forced in binary mode. This is also a default choice.'r+'
- reading mode plus writing mode at the same time. This allows you to read and write into files at the same time without having to user
andw
.'rb+'
- reading and writing mode in binary. The same asr+
except the data is in binary'wb'
- writing mode in binary. The same asw
except the data is in binary.'w+'
- writing and reading mode. The exact same asr+
but if the file does not exist, a new one is made. Otherwise, the file is overwritten.'wb+'
- writing and reading mode in binary mode. The same asw+
but the data is in binary.'ab'
- appending in binary mode. Similar toa
except that the data is in binary.'a+'
- appending and reading mode. Similar tow+
as it will create a new file if the file does not exist. Otherwise, the file pointer is at the end of the file if it exists.'ab+'
- appending and reading mode in binary. The same asa+
except that the data is in binary.
with open(filename, 'r') as f:
f.read()
with open(filename, 'w') as f:
f.write(filedata)
with open(filename, 'a') as f:
f.write('\n' + newdata)
What | r | r+ | w | w+ | a | a+ |
---|---|---|---|---|---|---|
Read | ✔ | ✔ | ✘ | ✔ | ✘ | ✔ |
Write | ✘ | ✔ | ✔ | ✔ | ✔ | ✔ |
Creates file | ✘ | ✘ | ✔ | ✔ | ✔ | ✔ |
Erases file | ✘ | ✘ | ✔ | ✔ | ✘ | ✘ |
Initial position | start | start | start | start | end | end |
Python 3 added a new mode for exclusive creation
so that you will not accidentally truncate or overwrite and existing file.
'x'
- open for exclusive creation, will raiseFileExistsError
if the file already exists'xb'
- open for exclusive creation writing mode in binary. The same asx
except the data is in binary.'x+'
- reading and writing mode. Similar tow+
as it will create a new file if the file does not exist. Otherwise, will raiseFileExistsError
.'xb+'
- writing and reading mode. The exact same asx+
but the data is binary
What | x | x+ | ||||
---|---|---|---|---|---|---|
Read | ✘ | ✔ | ✘ | ✔ | ✘ | ✔ |
Write | ✔ | ✔ | ✔ | ✔ | ✔ | ✔ |
Creates file | ✔ | ✔ | ✔ | ✔ | ✔ | ✔ |
Erases file | ✘ | ✘ | ✔ | ✔ | ✘ | ✘ |
Initial position | start | start | start | start | end | end |
Allow one to write your file open code in a more pythonic manner:
try:
with open("fname", "r") as fout:
# Work with your open file
except FileExistsError:
# Your error handling goes here
In Python 2 you would have done something like
import os.path
if os.path.isfile(fname):
with open("fname", "w") as fout:
# Work with your open file
else:
# Your error handling goes here
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents