
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
What are the modes a file can be opened using Python?
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
- Related Articles
- What does the 'b' modifier do when a file is opened using Python?
- What does the 'U' modifier do when a file is opened using Python?
- How to close an opened file in Python?
- What is the maximum file size we can open using Python?
- How to close all the opened files using Python?
- What are the types of Addressing Modes?
- What are the different modes of nutrition?
- What are the modes of operation in a helical antenna?
- What are the Selection Modes in a JTable with Java?
- What are the attributes of a file object in Python?
- How can Tensorflow be used to explore the dataset and see a sample file from the stackoverflow question dataset using Python?
- How we can truncate a file at a given size using Python?
- How can I source a Python file from another Python file?
- How are files added to a tar file using Python?
- How are files added to a zip file using Python?

Advertisements