Opens a file and returns it as a file object
open() function opens a file and returns it as a file object.
With that file object you can create, update, read, and delete files.
Read more about file handling here.
|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|
|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.
f = open('myfile.txt')
When you specify the filename only, it is assumed that the file is located in the same folder as Python.
You can also specify the exact path that the file is located at.
Example: Specifying absolute path
f = open('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
- double backslashes like
Specify File Mode
Here are five different modes you can use to open the file:
|‘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.
|‘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).|
Here are some examples:
Example: Open a file for reading
f = open('myfile.txt')
is same as
f = open('myfile.txt', 'rt')
Because read mode
'r' and text mode
't' are default modes, you don’t need to specify them.
Example: Open a file for reading and writing
f = open('myfile.txt', 'r+')
By specifying encoding parameter, you can decode or encode the file in popular encoding like
Example: Read a file in ‘UTF-8’ encoding
f = open('myfile.txt', encoding='UTF-8')
Example: 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.
|‘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
Example: Read a file in ‘ascii’ and ignore decoding errors
f = open('myfile.txt', encoding='ascii', errors='ignore') print(f.read())