Python File Handling

What is Python File Handling?

File handling is basically the management of the files on a file system. Every operating system has its own way to store files.

Python File handling is useful to work with files in our programs. We don’t have to worry about the underlying operating system and its file system rules and operations.

File Operations
File Operations

1. open() function

The open() function is used to open a file in a particular mode.

It basically creates a file object which can be used for further manipulations.


 open(file_name, mode) 

Different modes for opening a file:

  • r: Read
  • w: Write
  • a: Append
  • r+: Read and Write

Initially, we need to create a file and place it in the same directory as of the script.


Welcome to the programming world!

demo_file = open('Demo.txt', 'r')
# This statement will print every line in the file
for x in demo_file:
    print (x)

# close the file, very important


Welcome to the programming world!

Here, the script opens the Demo.txt file and prints the entire content line-by-line.

2. read() function

The read() function is used to read the contents of the file. To achieve the same, we need to open a file in the read mode.

demo_file = open("Demo.txt", "r")


Welcome to the programming world!

3. write() function

The write() function is used to write to a file and make changes to it.

demo_file = open('Demo.txt','w')
demo_file.write("Hello Everyone!.\n")
demo_file.write("Engineering Discipline.")

Output: When we open the Demo.txt file, we can see the changes reflected here.

Hello Everyone!.
Engineering Discipline.

4. append() function

demo_file = open('Demo.txt','a')

demo_file.write("\nStatement added to the end of the file..")


Hello Everyone!.
Engineering Discipline.
Statement added to the end of the file..

5. split() function

The split() function is used to split lines within a file. It splits up as soon as it encounters space in the script.


Hello Everyone!.
Engineering Discipline.
Statement added to the end of the file..

with open("Demo.txt", "r") as demo_file:
    demo_data = demo_file.readlines()
    for line in demo_data:
        result = line.split()


['Hello', 'Everyone!.']
['Engineering', 'Discipline.']
['Statement', 'added', 'to', 'the', 'end', 'of', 'the', 'file..']

6. close() function

The close() function is used to close a particular file post manipulations on it.

After writing to a file, if we do not call the close() method, all the data written to the file will not be saved in it.

It’s always a good idea to close the file after we are done with it to release the resources.



7. rename() function

The os module provides the rename() method to change the name of the particular file.



8. remove() method

The os module provides the remove() method to delete the file given as input.

import os

Before executing the remove() method..

Delete A File
Before remove()

Output: After executing the remove() method

After Remove
After remove()


Thus, in this article, we have understood the File operations in Python.