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

PythonServer Side ProgrammingProgramming

Files in python can be opened in the following modes.

 Mode Description 'r' Read mode. (default) 'w' Write mode. Creates a new file if it does not exist or truncates the file if it exists. 'x' Open a file for exclusive creation. If the file already exists, the operation fails. 'a' Appending at the end of the file without truncating it. Creates a new file if it does not exist. 't' Open in text mode. (default) 'b' 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.

## Example

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
Published on 27-Dec-2017 12:49:20