Python File Handling

Python 12 min min read Updated: Mar 09, 2026 Intermediate
Python File Handling
Intermediate Topic 6 of 10

File Handling in Python

File handling is an important feature in Python that allows programs to store data permanently on a system. Instead of keeping data only in memory during program execution, file handling enables developers to read, write, and update data in files.

Python provides built-in functions that make file handling simple and efficient. File operations are widely used in applications such as logging systems, data processing, configuration storage, and report generation.

What is File Handling?

File handling refers to the process of creating, reading, writing, and updating files using a programming language. Python provides the open() function to work with files.

python file = open("example.txt", "r") print(file.read()) file.close()

This program opens a file in read mode, reads its content, and then closes the file.

File Modes in Python

When opening a file, Python requires specifying the file mode. The mode determines the type of operation performed on the file.

Mode Description
r Read mode (default)
w Write mode (creates new file if not exists)
a Append mode (adds content to existing file)
x Create a new file
b Binary mode
t Text mode (default)

Reading Files

Python provides several methods to read data from a file.

read()

The read() method reads the entire content of the file.

python file = open("example.txt", "r") content = file.read() print(content) file.close()

readline()

This method reads one line at a time.

python file = open("example.txt", "r") print(file.readline()) file.close()

readlines()

This method reads all lines and returns them as a list.

python file = open("example.txt", "r") lines = file.readlines() print(lines) file.close()

Writing to Files

The w mode allows writing data to a file. If the file already exists, its content will be overwritten.

python file = open("example.txt", "w") file.write("Welcome to Python File Handling") file.close()

Appending Data to Files

The append mode a adds new data to the end of the file without deleting existing content.

python file = open("example.txt", "a") file.write("\nThis line is added later.") file.close()

Using the with Statement

The with statement is the recommended way to work with files because it automatically closes the file after the operation is completed.

python with open("example.txt", "r") as file: content = file.read() print(content)

This approach prevents errors caused by forgetting to close files.

Checking if a File Exists

Before reading a file, it is good practice to check whether the file exists.

python import os if os.path.exists("example.txt"): print("File exists") else: print("File does not exist")

Deleting Files

Python can also delete files using the os module.

python import os os.remove("example.txt")

Real-World Example

File handling is widely used in applications such as storing user logs or saving data.

python with open("log.txt", "a") as file: file.write("User logged in\n")

This program appends login information to a log file.

Best Practices for File Handling

  • Always close files after use.
  • Use the with statement whenever possible.
  • Handle exceptions to avoid program crashes.
  • Check file existence before reading or deleting.

Conclusion

File handling is an essential part of Python programming that allows developers to store and retrieve data from files. Python provides simple and powerful methods for reading, writing, and managing files.

By mastering file handling, developers can build applications that store logs, manage configurations, process data files, and interact with external data sources.

In the next tutorial, we will learn about Exception Handling in Python and understand how Python manages runtime errors.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators