There 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 to
r 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 use
'rb+' - reading and writing mode in binary. The same as
r+ except the data is in binary
'wb' - writing mode in binary. The same as
w except the data is in binary.
'w+' - writing and reading mode. The exact same as
r+ 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 as
w+ but the data is in binary.
'ab' - appending in binary mode. Similar to
a except that the data is in binary.
'a+' - appending and reading mode. Similar to
w+ 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 as
a+ 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)
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 raise
FileExistsErrorif the file already exists
'xb'- open for exclusive creation writing mode in binary. The same as
xexcept the data is in binary.
'x+'- reading and writing mode. Similar to
w+as it will create a new file if the file does not exist. Otherwise, will raise
'xb+'- writing and reading mode. The exact same as
x+but the data is binary
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