In Python file handling is an important aspect that performs multiple operations. These operations include creating, opening, reading, and updating the files. Among these operations, the most common operations are reading the file and writing to the file. These various operations in Python can be implemented on two types of files,  the text files and the binary files. 

In this article, first, we will go through the open() function and with statement separately and then the article will further advance to open the file using the combination of both.

The open() Function of Python

The open() function in Python opens the files and returns the contents of the file. This function consists of two main parameters which are the name of the file and the mode of the file. The file name that is to be opened is passed as an initial parameter and the mode in which the file is to be opened is passed as the second parameter. The syntax of the open() function is (file_name, mode). 

The modes in the above syntax are depicted in the table below:

S.NoModeWorking/Functionality 
1rThis reads the file and is a default parameter. It returns an error if the file doesn’t exist.
2bThis mode is used for binary files. The main purpose of this mode is to handle images.
3wThis mode is used to write over a file. If the file required doesn’t exist, it creates a new one
4aThis mode appends the content to the file already created.
5xIt is used for the creation of a specific file.
6tIt is referred to as the text mode. The open() function by default uses the text mode of the file.

The with Statement in Python

The with statement in Python is used to create a context. It basically provides a better way for exception handling and ensures that certain processes don’t disturb the main process and terminate smoothly. 

Now let us advance with the implementation of the with open statement in Python.

How to Open a File Using the with open Statement in Python?

The open with statement is a combination of the open() function and the with statement. This involves creating a file and opening it when required. We have first created a file below as “file1.txt” and the content is added to it on separate lines. The with open statement will execute to open the file. 

Method 1: with open() Basic Implementation

The code below shows the basic implementation of the with open statement in Python. 

#Open() function and the with statement
with open("C:\\Users\\KHALID HAMEED\\Desktop\\file1.txt","r") as text_file:
    #Read the file
    file_content = text_file.read()
    #Print the contents of the files
    print(file_content)

In the above block of code:

  • The with open statement takes the path of the file and reads it as a text file.
  • The read() function reads the contents of the file.
  • The contents in the file are printed using the print() function.

Output

The output below shows that the contents in the file are printed on Python Shell.

Method 2: with open Statement to Append the Content in File

#Open() function and the with keyword
with open("C:\\Users\\KHALID HAMEED\\Desktop\\file1.txt","a") as text_file:
    text_file.write("\nPython is easy to Learn")
    #Read the file and verify
with open("C:\\Users\\KHALID HAMEED\\Desktop\\file1.txt","r") as text_file:
    file_content = text_file.read()
    #Print the contents of the files
    print(file_content)

In the above code block, the first with an open statement has an argument “a” which means that certain content is to be appended. In order to verify the file is read again using the parameter “r” of the open() function which means to read the contents of the file.

Output

The output below shows that a new line “Python is easy to Learn” is added to the text file.

Method 3: Using the for Loop

The for loop iterates over the contents in the file. Using the with open statement and the for loop, the file is opened in Python.

#Open() function and the with keyword
with open("C:\\Users\\KHALID HAMEED\\Desktop\\file1.txt","w") as text_file:
    text_file.write("\nPython is easy to Learn")
    #Print the contents of the files
with open("C:\\Users\\KHALID HAMEED\\Desktop\\file1.txt") as text_file:
    for content in text_file:
        print(content)

In the above code, a line is written to the file, and using the for loop the content is printed on the screen.

Output

The output below shows that the line is printed on the screen as the output.

Conclusion

The with open statement in Python implements the open() function using different modes with a combination of with statement to open a file. In this write-up, we have demonstrated different methods to open a file in Python.