Python - File Handling

In real-world application development, the logic that we implement uses some data and those data may come from different sources like databases, files, or API. As a python developer, we should have good knowledge of handling these different sources. In this tutorial, we will see how we can handle files and perform some operations using python in-built functions.

Basically, a file is a location to store information in non-volatile memory (Hard Disk). And, it is characterized by its name and extension.

When we want to perform some operation(Read/Write/Update) in a file, we will open it first. Then we perform our task and finally, we will close it to save the changes and deallocate the resources tied with the file.

Similarly, in python, File operation takes place in the following order:

  1. Open a file
  2. Perform operation (Read/Write/Update)
  3. Close the file
Opening a file in python:

Python provides an open() function that accepts two arguments, file_name_path and access_mode in which the file has to be accessed. open() function returns a file object using which file operations like reading, writing, and closing are done.

Syntax: file_object = open(file_name_path , access_mode)
  • file_name_path → file name or path of the file to be opened.
  • access_mode (Optional) → mode in which the file has to be opened. If not specified, default mode will be read.
  • file_object → Object returned by open which is used for further file operations.

>>> f = open("Data.txt","r")    # open file in current directory, read mode

>>> f = open("C:/Python/test.txt","w")  # specifying full path, write mode
Access modes:

There are different modes for opening a file. Those are 
  • "r" → Read - Opens a file for reading (default), error if the file does not exist.
  • "w" → Write - Opens a file for writing. Truncates the file if already exists or creates a new file if it doesn't exist.
  • "a" → Append - Opens a file for appending at the end of the file without truncating. Creates a new file if it doesn't exist.
  • "x" → Create - Creates the specified file, returns an error if the file exists.
  • "+" → Opens a file for updating (reading and writing)
    • "r+" → Read and write a file. Previous data will not be deleted.
    • "w+" → Write and read a file. Override the existing data.
    • "a+" → Append and read a file. Previous data will not be deleted.
In addition, we can also specify whether the file should be handled in text or binary mode.
  • "t" → Text - Opens in text mode (default)
  • "b" → Binary - Opens in binary mode.
Binary mode returns byte and this mode is used for binary files like images, executable files.

Closing a file in python:

After completion of all the file operations, we need to close the file to deallocate the resources tied with the file. It is done using the close() method available in python.

Syntax: file_object.close()

>>> f = open("Data.txt", 'r') #Opens the file

# perform file operations

>>> f.close() #Closes the file
Even though this method closes the file. Using the close method straight away is not recommended, In case an exception occurs in the file operation (read or write) then the code will exit without closing the file.

Best way to close file is using try...finally block

   f = open("Data.txt", 'r') #Opens the file
   # perform file operations
   f.close() #Closes the file
We can also use "with" statement to close a file Since this ensures that the file is closed when the block inside the "with" statement is exited. No need to call the close() method explicitly.

with open("Data.txt", 'r') as f:
   # perform file operations
Reading Files:

Consider a file data.txt with sample data

Python - File Handling

Reading Single line - readline():

readline() function is to read the single line from the file at a time. When the end of the file is reached, it will return an empty string.

f = open("Data.txt","r")

line1 = f.readline()
print(line1, end="")

line2 = f.readline()
print(line2, end="")

line3 = f.readline()
print(line3, end="")


First line: Hello World
Second line: Data file
Third line: Reading Operation
Reading a file contents into a list - readlines():

readlines() function is to read entire content of the file into a list whereas each line will be present as an element of the list.

f = open("Data.txt","r")

contents = f.readlines()

for line in contents:


First line: Hello World
Second line: Data file
Third line: Reading Operation
Reading contents of a file into a string - read(size):

read(size) function is to read the specified size(number) of characters from the file as a string. If no size is specified, then the entire content of the file is read as a string.

f = open("Data.txt","r")

data =

First line
Writing to Files in Python:
  • To write a file in python, we can use the write() method. And also, we must need to specify the access mode "w"/"a" to the open() function.
  • "w" mode overwrites the existing data if the file is already present or creates if the file doesn't exist. And, it will also return the number of characters written into a file.
f = open("Data.txt","r")
data =
print("Before Writing")

f = open("Data.txt","w")
num1 = f.write("Write operations performed\n")
num2 = f.write("lines written using write method\n")
print("Total number of characters written:",num1+num2)

f = open("Data.txt","r")
data =
print("After Writing")

Before Writing
First line: Hello World
Second line: Data file
Third line: Reading Operation
Total number of characters written: 60
After Writing
Write operations performed
lines written using write method
  • Here, I have used the existing file with write mode, which results in overwriting the existing data, and the write method also returned the number of characters added to the file.
  • And, We must include the newline characters explicitly to distinguish the lines.

File Methods:
  • tell() → get the current position which is pointed by the file object within the file.
  • seek() → Navigate the file object pointer to the required position specified.

No comments:

Post a Comment

You might also like

Deploy your Django web app to Azure Web App using App Service - F1 free plan

In this post, we will look at how we can deploy our Django app using the Microsoft Azure app service - a free plan. You need an Azure accoun...