file handling in python

File Handling in Python: Understand Step-by-Step With Examples

Jashan
October 16th, 2026
10664

File handling in Python is a method of interacting with files on the local file system. It involves performing different operations like creating, opening, reading, writing, appending and closing them. It is important for storing and retrieving data persistently, which helps to manage configurations, handle logs and process large datasets.

Understanding this concept is very important for Python developers, data analysts, web developers and system administrators. Each of them deals with these files. I have designed this comprehensive guide to explain the complete process from opening and writing to closing and managing potential errors. Let’s begin:

What is File Handling in Python?

File handling in Python is the process of working with files on secondary storage systems, like a hard drive. It uses built-in functions to perform operations such as creating, reading, updating and deleting files.

It is used to manage external data stored in files. It also helps Python programs to interact with the persistent data on your disk. This makes it possible to store the output of your program for later use or to read input data for processing.

Why Learn File Handling?

Learning file handling is important because it moves your programs beyond temporary data storage. Here are some of the common reasons behind its importance:

Reasons Explanation
Data Persistence - Variables are destroyed when a program terminates. - Files allow you to store data permanently.
External Configuration - You can read configuration settings like API keys or database credentials from a file.
Log Files - Programs create log files to record events, errors and system status for debugging and monitoring.
Data Processing - You can read large datasets from CSV, JSON or text files for analysis.

Also read: Python Tutorial

Learn Python. Turn knowledge into skills.

Build your Python programming foundation through practical training, coding exercises, and hands-on projects designed for real-world applications.

Python Basics Functions & OOP Problem Solving
Enroll in Python Training →

Types of Files in Python

Python generally deals with two main types of files and both of them require a specific approach, including:

1. Text Files

Text Files in Python store data in a sequence of characters. This data is mostly encoded in a format like ASCII or UTF-8 and is easy to read. You can find them in .txt, .py, .csv or .html format. It is also easy to work with text files, as the Python runtime automatically handles the encoding/decoding process.

2. Binary Files

Binary files in Python store data in a sequence of raw bytes. These are not easy to read and can be found in and include file types like images (.jpg, .png), video, compiled code and compressed data (.zip). Python passes this data without any encoding conversion while working with binary files.

How to Open Files in Python?

Opening the files is the first operation one must do to manage them. Python's built-in function open() is used for this and returns a file object. It acts as the link between your program and the file. The syntax for the open() function is:

file_object = open(file_name, access_mode)

The key to file handling is the access mode, which specifies the type of operation you intend to perform. The table given below entails these mods and their intent:

Mode Description Action if File Exists Action if File Does NOT Exist
'r' Read (Default) Cursor placed at the beginning. Raises a FileNotFoundError.
'w' Write Overwrites the entire file. Creates a new file.
'a' Append Cursor placed at the end. Creates a new file.
'r+' Read and Write Cursor placed at the beginning. Raises a FileNotFoundError.
'w+' Write and Read Overwrites the entire file. Creates a new file.
'a+' Append and Read Cursor placed at the end. Creates a new file.

Example: Opening a file for reading

try:
    file = open("my_data.txt", "r")
    print("File opened successfully.")
except FileNotFoundError:
    print("Error: The file does not exist.")

Also explore related articles: Python MCQs

Build in-demand skills in Data Science & BI.

Explore data science and business intelligence training to develop practical skills in data analysis, visualization, Python, and business reporting.

Data Science Business Intelligence Data Analytics

How to Write Files in Python?

You can write anything in a file once it is opened in a writing ('w', 'a', 'w+', 'a+') mode. Use the file object's write() method for it. The write() method takes a string as an argument and writes it to the file. It is important to remember that write() does not automatically add a newline character (\n). You must include it manually if you want content on separate lines.

Example: Writing content to a file using the 'w' (write) mode

# Open in 'w' mode (will create the file or overwrite existing content)
file = open("my_output.txt", "w")

# Write some content
file.write("First line of text.\n")
file.write("Second line, written in Python.")

# Close the file to ensure data is saved
file.close()

If you ran this code again, the first line would overwrite the file and only the two lines above would remain.

Example: Appending content to a file using the 'a' (append) mode

# Open in 'a' mode (will add to the end of existing content)
file = open("my_output.txt", "a")

# Append a new line
file.write("\nThis line was appended later.")

file.close()

How to Close Files in Python?

It is the most important step in file handling. You must call the close() method on the file object to ensure all changes are saved and the file is released. Failing to close a file can lead to:

1. Data Loss/Corruption: Data written to the file might be temporarily buffered and not actually saved to disk until the file is closed.

2. Resource Leaks: The operating system has a limit on the number of files a program can have open simultaneously. Unclosed files consume system resources.

3. File Locking: Other programs might not be able to access or modify the file until your program closes it.

Example: Explicitly closing a file

file = open("demo.txt", "w")
# ... operations ...
file.close() # Always remember to close!

How to Use With Statements?

Wherever you explicitly call file.close(), it always shows an error. This error occurs between the open() and close() calls, which may crash the program before reaching the close() statement. The best way to manage file operations is using the with statement in conjunction with the open() function. The with statement uses context managers and guarantees that the close() method is automatically called.

Example Syntax using with statement

with open("my_file.txt", "r") as file:
    # Operations go inside this block
    content = file.read() 

# The file is automatically closed as soon as the block is exited
print(content) 
print(file.closed) # Output: True

The file object is assigned to the variable defined after the as keyword. Python handles the cleanup automatically once the indented block is finished. This makes your code safer and cleaner.

Handling Exceptions When Closing a File

Closing a file explicitly can also lead to an exception that could prevent the code from reaching file.close(). The most robust way to manage this without the with statement is by using a try...finally block. The finally block is guaranteed to execute even if an exception occurred in the try block.

file = None
try:
    # 1. Open the file
    file = open("dangerous_operation.txt", "w")
    
    # 2. Perform a potentially risky operation
    file.write("Writing data...")
    # Imagine a ZeroDivisionError happens here...
    
except Exception as e:
    print(f"An error occurred: {e}")
    
finally:
    # 3. Ensure the file is closed, regardless of errors
    if file is not None:
        file.close()
        print("File successfully closed in finally block.")

File Management Principles

Beyond reading and writing, the os and shutil modules provide powerful tools for file management:

Module Purpose Key Function Example
os Interacting with the operating system (paths, environment, etc.) os.remove("old_file.txt") (Delete a file)
os.path Checking and manipulating file paths os.path.exists("file.txt") (Check if a path exists)
shutil High-level file operations (copying, moving) shutil.copy("src.txt", "dest.txt") (Copy a file)

Example: Deleting a file

import os

if os.path.exists("my_output.txt"):
    os.remove("my_output.txt")
    print("File deleted successfully.")
else:
    print("The file does not exist.")

Explore the power of Python and AI.

Learn Python programming and artificial intelligence through practical training, hands-on projects, and real-world applications to build skills for AI-driven careers.

Python Programming Artificial Intelligence Machine Learning
Enroll in Python with AI Course →

FAQs: File Handling in Python

Q1. What is the default mode for the open() function in Python?

The default access mode for the open() function is 'r' (read mode) for text files. Python attempts to open the file for reading after you omit the mode argument.

Q2. What is the difference between read(), readline() and readlines()?

These are methods used on a file object opened in read mode:

  • read(): Reads the entire content of the file and returns it as a single string.
  • readline(): Reads one single line from the file and returns it as a string.
  • readlines(): Reads all lines from the file and returns them as a list of strings where each element is one line.

Q3. How do I create a new file in Python without overwriting an existing one?

Use the exclusive creation mode 'x'. This mode is specifically designed to create a new file.

Wrapping Up

File Handling in Python is a foundational concept that bridges your running program with the permanent data storage of your computer. By understanding the different access modes, utilizing the robust with statement for automatic cleanup, and managing your files with the os module, you can build powerful and reliable applications. Always default to the with open(...) construct. It is the best practice for secure and effective file management.

Learn Other Python Concepts with Our Comprehensive Guides:-

About the Author
Jashan | igmGuru
About the Author

Jashan has written production code in multiple languages, with a particular focus on Python and R for data-heavy applications. Debugging late-night production issues shaped his opinions on maintainable code. His writing draws on real projects like automation scripts and data pipelines, helping programmers build habits that hold up under real deadlines.

Drop Us a Query
Fields marked * are mandatory
×

Your Shopping Cart


Your shopping cart is empty.