What are the modes a file can be opened using Python?

Files in python can be opened in the following modes.

Read mode. (default)
Write mode. Creates a new file if it does not exist or truncates the file if it exists.
Open a file for exclusive creation. If the file already exists, the operation fails.
Appending at the end of the file without truncating it. Creates a new file if it does not exist.
Open in text mode. (default)
Open in binary mode.
Open a file for updating (reading and writing)


These modes can be used in combinations and need to be passed as the second argument when opening a file. If you don't specify a mode, files are opened in readonly text mode.


f = open("test.txt") # Equivalent to rt or race
f = open("test.txt", 'w') # Write in text mode
f = open("test.bmp", 'r+b') # Read/write in binary mode
f = open("test.txt", 'a') # Append mode