Python open() Function
The open() function opens a file and returns it as a file object. With this file object you can create, update, read, and delete files.
Read more about file handling here.
Syntax
open ( file , mode , buffering , encoding , errors , newline , closefd , opener )
Parameter | Condition | Description |
file | Required | The path and name of the file |
mode | Optional | Specifies the mode you want to open the file in |
buffering | Optional | Sets the buffering policy |
encoding | Optional | Specifies encoding |
errors | Optional | Specifies different error handling scheme |
newline | Optional | Controls how universal newlines mode works |
closefd | Optional | Keeps the underlying file descriptor open when the file is closed |
opener | Optional | A custom opener used for low-level I/O operations |
Open a File
You can open a file using open() built-in function specifying its name.
When you specify the filename only, it is assumed that the file is located in the same folder as Python. If it is somewhere else, you can also specify the exact path that the file is located at.
# Specifying absolute path f = open(r'C:\Python33\Scripts\myfile.txt')
Remember! While specifying the exact path, characters prefaced by \ (like \n \r \t etc.) are interpreted as special characters.
You can escape them using:
Specify File Mode
Here are five different modes you can use to open the file:
Character | Mode | Description |
‘r’ | Read (default) | Open a file for read only |
‘w’ | Write | Open a file for write only (overwrite) |
‘a’ | Append | Open a file for write only (append) |
‘r+’ | Read+Write | open a file for both reading and writing |
‘x’ | Create | Create a new file |
You can also specify how the file should be handled.
Character | Mode | Description |
‘t’ | Text (default) | Read and write strings from and to the file. |
‘b’ | Binary | Read and write bytes objects from and to the file.This mode is used for all files that don’t contain text (e.g. images). |
# Open a file for reading f = open('myfile.txt') # Open a file for writing f = open('myfile.txt', 'w') # Open a file for reading and writing f = open('myfile.txt', 'r+') # Open a binary file for reading f = open('myfile.txt', 'rb')
Because read mode ‘r’ and text mode ‘t’ are default modes, you do not need to specify them.
Specify Encoding
By specifying encoding parameter, you can decode or encode the file in popular encoding like ‘ascii’ , ‘UTF-8’ etc.
# Read a file in 'UTF-8' encoding f = open('myfile.txt', encoding='UTF-8')
# Read a file in 'ascii' encoding f = open('myfile.txt', encoding='ascii')
Handling Encoding and Decoding Errors
By default, Python raises UnicodeError exception on encoding or decoding errors. However, you can specify how these errors are to be handled using errors parameter.
Below table specifies different error handling schemes.
Parameter value | Description |
‘strict’ (Default) | raises an UnicodeError exception on failure |
‘backslashreplace’ | the unencodable character is replaced by a backslash |
‘ignore’ | the unencodable character is ignored |
‘namereplace’ | the unencodable character is replaced by its name |
‘replace’ | the unencodable character is replaced by questionmark |
‘xmlcharrefreplace’ | the unencodable character is replaced by an xml character |
Following example shows how the German letter ß is ignored while reading the file in ‘ascii’ encoding.
# Read a file in 'ascii' and ignore decoding errors f = open('myfile.txt', encoding='ascii', errors='ignore') print(f.read()) # Prints Das strae