How to Open a File in Python?

File handling is an important task in programming languages. In Python, various functions are used to perform operations on files. Some of the operations include reading, writing, removing, appending, etc.

To open a file the “open()” function is used in Python. Users can open files in various modes, such as read, write, append, etc, based on the operation that needs to be performed on the file.

This post will guide you on how to open a file in Python using different file modes. The following concepts will be explained in this guide:

How to Open a File in Python Using the open() Function?

The “open()” function is utilized to open/create a file in Python. Below is the syntax of the “open()” function:

open(file, mode)

The “open()” function accepts two parameters i.e. file and mode. There are several modes to open a file in Python, such as “r”, “a”, “w”, etc. Use the “a” mode to append the text to a file, the “w” mode to write text to the file, the “r” mode to read the file, etc.

Let’s understand it by the following examples:

Example 1: Open a File in Read Mode

The content of the sample file is shown in the below snippet:

In the below code the “open()” function is used to open the file in read mode:


file_name = open("sample.txt", 'r')
  • The file named “sample.txt” is opened in read mode utilizing the “open()” function.
  • The “” is used to read the content of the file.
  • To close the file the “file_name.close()” is used.


The given file has been read using the “” function.

Example 2: Open a File in Append Mode to Add Data

In the below code, the “open()” function is used to append the data to a file:


file_name = open("sample.txt", 'a')
file_name.write("\nPython Guide\nLinux Guide")
  • Firstly, the “open()” function accepts the file name and the mode “a” to open the file.
  • The “file_name.write()” function accepts the string value and writes it into the file named “sample.txt”.


The specific content has been appended to a file.

Example 3: Open a File in Write Mode

The following code opens a file in write mode:


file_name = open("sample.txt", 'w')
file_name.write("Python | Java | Linux")
  • The “open()” function takes the file name “sample.txt” and mode “w” as an argument to open the file for writing.
  • The function named “write()” is utilized to write the string content to the file named “sample.txt”.


The specific content has been written to a file named “sample.txt”.

Example 4: Open a File Using With open() Function

The below code shows how to use the “with open()” function to open a file in Python:


with open("sample.txt", "r") as f:
    data =
  • The “with open()” function is used to open the file named “sample.txt” in read mode.
  • The “read()” function is utilized to read the data.
  • When we open the file using the “with” keyword we don’t need to close the file because the file closes automatically.


The file has been opened successfully.


To open a file, the “open()” function is used in Python. The file is opened in various modes such as read mode, write mode, append mode, etc. To open the file the “with” keyword is used along with the “open()” function. The file opened using the “with open()” function automatically closes after performing the operation. This tutorial provided a complete guide on how to open a file in different modes in Python.