In python, there are several ways for reading, writing and closing a file. Such as by using Numpy or Pandas libraries. However, Python also has its own built-in function to read or handle a file. Throughout this article, we will learn about python file handling, for which we will use the open() and then with open() function to open and the read() method to read and finally, the close() method to close the file.

Text File Opening in Python – open()

We need to use the open() function to open a file via python programing language. Mostly, the open() function will return a file object. If the file cannot open, it will return an error.

Syntax

file = open("file_path/filename with extention", mode = "", encoding="") 

# if .py file and our desire file are in the same directory, then no need to 
# mention the filepath
file = open("only filename with extention", mode = "") 

Notes:

As we can see open() function takes three parameters:

  1. file_path/file_name: Need to insert the name of the file with the extention
  2. Mode: This parameter explain how file will be oppened. such as read, write, append or create mode.
  3. Encoding: to read the UTF-8 text file it is mandatory to use the encoding=’utf-8′.

Code

file = open("C:\Users\folder\Documents\test.txt", mode = "r") 
#If both files are saved in the same directory code, therefore, no need to mentipn the filepath
file = open("test.txt", mode = "r") 

The open() function will default take “r” as a mode value. As a result, we can skip the mode parameter If the purpose is only to read a file. Therefore, the rewritten code will be the same as below.

file = open("test.txt") 

Text File Reading in Python – read()

Once a file is opened, different methods can be used. For this article, we will use only the read() method before using the print() function to view the element of that file.

contents = file.read()
print(contents)
#We can also write like this way
print(file.read())

Text File Closing in Python- close()

After reading a file, It is crucial always to close the file to avoid any future disturbance. To do that, we will use the close() method.

file.close()

Example

file = open("test.txt")
contents  = file.read()
print(contents)

file.close()

Output:

This is a new line.

Open a text file – with open()

As per our above code, we have used the close() method to close the file. To avoid calling the close() method after opening a file, let’s add the “with” keyword before the “open()” function. Therefore, python will close the file by itself, and our code will look like this:

with open("test.txt") as file:
    contents = file.read()
    print(contents)

Output

This is a new line.

To check if our file is closed, we can write it like below:

file.closed

Output

True

Summary of Python file handling:

  1. Use the open() function to a file and the close() method.
  2. Or, use with open() function to open. There is no need to use the close() method.
  3. An open () function has default “r” mode; therefore, No need to use mode if the object is only to read a file.
  4. To read a file, need to use the read() method.
  5. Use closed to check whether the file is open or closed.