1r for reading
2r+ opens for reading and writing (cannot truncate a file)
3w for writing
4w+ for writing and reading (can truncate a file)
5rb for reading a binary file. The file pointer is placed at the beginning of the file.
6rb+ reading or writing a binary file
7wb+ writing a binary file
8a+ opens for appending
9ab+ Opens a file for both appending and reading in binary. The file pointer is at the end of the file if the file exists. The file opens in the append mode.
10x open for exclusive creation, failing if the file already exists (Python 3)
1Mode: Description:
2"r" # Opens a file for reading. (default)
3"w" # Opens a file for writing. Creates a new file if it does not exist or truncates the file if it exists.
4"x" # Opens a file for exclusive creation. If the file already exists, the operation fails.
5"a" # Opens a file for appending at the end of the file without truncating it. Creates a new file if it does not exist.
6"t" # Opens in text mode. (default)
7"b" # Opens in binary mode.
8"+" # Opens a file for updating (reading and writing)
1# Different modes of text file
2"r" = # Open for reading plain text
3"w" = # Open for writing plain text
4"a" = # Open an existing file for appending plain text
5"rb" = # Open for reading binary data
6"wb" = # Open for writing binary data
1>>> with open('workfile') as f:
2... read_data = f.read()
3
4>>> # We can check that the file has been automatically closed.
5>>> f.closed
6True
7