Python open() Function

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Overview

The open() function in Python opens a file stored at a specific location in the system and returns the file object.

Python open() Function

The Python open() function opens the file in different modes like reading, writing, appending, creating, etc, and returns the file as an object.

In the Python open() function you can also specify if the file should be handled as a binary or text mode.

If the file is not found then the open() function raises the FileNotFoundError Exception.

Syntax of Python open() Function

The syntax of the Python open() function is :

The open function takes many parameters for different purposes which are further explained in this article.

Parameters of Python open() Function

The different parameters passed to open() function in Python are :

  • file: It is a path-like object which represents the file system path.
  • mode: It specifies the mode in which the file should be opened. If this parameter is not passed it takes the default value as 'r' i.e read mode. The file gets opened for reading in text mode by default.

Other available file modes are :

ModeDescription
'r'opens file in reading mode(default)
'w'opens the file in writing mode. If the file does not exist create a new file else truncate the existing file
'x'open the file for exclusive creation. If it already exists then the operation fails
'a'open the file for appending it at the end of the file without truncating the file. If the file does not exist then a new file is created
't'open file in text mode
'b'open file in binary mode
'+'open file for updating i.e reading and writing
  • buffering : It sets buffering policy in open() Python function.
  • encoding : The encoding format
  • errors: handle encoding/decoding error
  • newline: enters the new line mode.(values available are ' ', '\n', 'r', and '\r\n')
  • closefd: its value is true by default. If any other value is given then an exception is raised.
  • opener : a custom opener. It returns an open file descriptor.

Return Value of Python open() Function

The open() Python function returns a file object which is used to read, write or modify the file when different parameters are passed as a mode to the open() Python function.

If the file is not found then the open() Python function raises FileNotFound Error.

Working of open() Function in Python

open() Python function is a built-in Python function. It returns a file object which can be used to do operations like reading, writing, or modifying the file. The different modes for file reading, writing, and modification are passed as mode parameters to the open() Python function.

Other parameters like buffering, encoding, errors, etc can be passed to an open function to do different operations on the file.

Uses of open() Function

open() Python function is used to read, write and modify operations on the file.

Python open() Function Examples

Example 1: Open a file in Python

In the above code, no mode is passed as a parameter to the open function so it opens the file in reading mode by default.

Example 2: Opening file in a specific mode in Python

Example 3: opening file in different encoding in Python.

The default encoding in Python is ASCII. In the above program, the file encoding is changed to utf8 using the encoding parameter in the open Python function.

You can read more about different Python functions here

Conclusion

  • open() Python function opens a file and returns a file object.
  • open function is used to read, write and modify a file.
  • File can be opened in different modes like reading, writing, and append modes by passing the mode parameter to the open function.
  • If the file passed to the open function is not found then it raises a FileNotFound error.
  • open Python function returns a file object.