Python open() Function

Opens a file and returns it as a file object

Usage

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)

Python open() function parameters
ParameterConditionDescription
fileRequiredThe path and name of the file
modeOptionalSpecifies the mode you want to open the file in
bufferingOptionalSets the buffering policy
encodingOptionalSpecifies encoding
errorsOptionalSpecifies different error handling scheme
newlineOptionalControls how universal newlines mode works
closefdOptionalKeeps the underlying file descriptor open when the file is closed
openerOptionalA 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.

f = open('myfile.txt')

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:

  • raw strings like r'C:\new\text.txt'
  • double backslashes like 'C:\\new\\text.txt'

Specify File Mode

Here are five different modes you can use to open the file:

Python File Modes
CharacterModeDescription
‘r’Read (default)Open a file for read only
‘w’WriteOpen a file for write only (overwrite)
‘a’AppendOpen a file for write only (append)
‘r+’Read+Writeopen a file for both reading and writing
‘x’CreateCreate a new file

You can also specify how the file should be handled.

Python File Modes
CharacterModeDescription
‘t’Text (default)Read and write strings from and to the file.
‘b’BinaryRead and write bytes objects from and to the file.This mode is used for all files that don’t contain text (e.g. images).

Here are some examples:

# 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.

Encoding error handling schemes
Parameter valueDescription
‘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.

myfile.txt
Das straße
# Read a file in 'ascii' and ignore decoding errors
f = open('myfile.txt', encoding='ascii', errors='ignore')
print(f.read())

# Prints Das strae