Skip to main content
Taught by Tech Leads

Master pipelines, cloud & AI to become an operational Data Engineer.

DataScientist.fr
Image de File Handling in Python - Complete Tutorial
Python

File Handling in Python - Complete Tutorial

Photo de Romain DE LA SOUCHÈRE

Tech Lead, CTO AXI Technologies

Published on 8 janvier 2025 · 10 min of reading

Introduction to File Handling in Python

File handling is an essential skill for any Python developer. Whether you are a beginner or experienced, understanding how to open, read, write, and manage files is crucial for developing many applications. Files allow for persistent data storage, exchanging information between programs, and even processing large volumes of data.

Why handle files in Python?

Python offers a rich standard library for file handling, making these operations simple and efficient. With built-in functions like open(), you can access files on your system, read them, modify them, and close them easily.

Common use cases

File handling in Python is useful in various domains:
  • Data Processing: Reading and writing CSV, JSON, and XML files.
  • Automation: Creating scripts to automate repetitive tasks.
  • Log Analysis: Analyzing log files for debugging and monitoring.
In this article, we will explore these concepts in detail, starting from the basics of opening and closing files to advanced file handling techniques. You will discover practical examples and use cases that will enable you to master file handling in Python.

Opening and Closing Files

Opening and closing files in Python are fundamental operations that form the basis of any file handling. To do this, we mainly use the open() function.

Using the open() function

The basic syntax for opening a file is as follows:
python
The mode parameter specifies the file opening mode:
  • 'r': Read-only.
  • 'w': Write (creates a new file or overwrites the existing one).
  • 'a': Append (writes to the end of the file).
  • 'b': Binary mode (added to other modes like 'rb' or 'wb').

Practical Example

To open and read a text file:
python

Closing Files

It is crucial to close a file after use to free up resources:
python

Using the with context

A safer and more efficient way to open a file is to use a with statement:
python
The advantage of this method is that the file is automatically closed at the end of the block, reducing the risk of resource leakage.
These basic concepts allow you to start manipulating files in Python in a secure and efficient manner.

File Reading and Writing Techniques

Once you know how to open and close files, it's time to learn how to read and write data. Python offers several methods for this, each suited to specific needs.

Reading Files

Read the entire content:
python
Read line by line:
python
Read into a list:
python

Writing Files

Write text:
python
Append text:
python

Binary Reading and Writing

For non-text files, use binary mode:
python
These techniques cover the basic operations of reading and writing, essential for any file handling in Python.

Navigating the File System with Python

Navigating the file system is a crucial skill for effectively managing files and directories. Python provides several modules for this task, including os and os.path.

Changing Directory

To change the current working directory:
python

Getting the Current Directory

To display the current working directory:
python

Listing Files and Directories

To list the contents of a directory:
python

Creating and Deleting Directories

To create a new directory:
python
To delete an empty directory:
python

Managing File Paths

The os.path module allows you to manipulate file paths in a portable manner:
python
You can also check for the existence of a file or directory:
python
These tools allow you to navigate and manage the file system effectively, making your Python scripts more robust and versatile.

Advanced File Manipulation

Advanced file manipulation in Python involves more complex operations like copying, moving, recursive deletion, and permission management. For this, we mainly use the shutil and os modules.

Copying and Moving Files

To copy a file:
python
To move a file:
python

Recursive Deletion of Directories

To delete a directory and all its contents:
python

Changing File Permissions

Change the permissions of a file:
python

Reading and Writing Metadata

To obtain detailed information about a file:
python

Managing Temporary Files

To create and use temporary files:
python
These advanced techniques allow for finer and more flexible file management, essential for robust and professional applications.

Practical Examples and Use Cases

To illustrate the use of file handling techniques in Python, here are some practical examples and use cases.

Example 1: Processing CSV Files

Read a CSV file and calculate the average of a column:
python

Example 2: Merging Multiple Text Files

Combine the contents of several text files into one:
python

Example 3: Cleaning Directories

Delete temporary files from a directory:
python
These examples demonstrate how file handling techniques can be applied to common tasks, thus improving the efficiency and automation of your Python scripts.

Want to go further?

This topic is part of our Become a Data Analyst course. Browse the full programme, or get it by email.

Share with

Photo de Romain DE LA SOUCHÈRE

Romain DE LA SOUCHÈRE

Tech Lead, CTO AXI Technologies

Expert Data Engineering et Cloud, Romain affiche plus de 11 ans d'expérience, dont plusieurs années comme Lead Developer sur des solutions Smart Building haute performance. Il y a conçu et mis en production des moteurs de traitement capables d'absorber des centaines de milliers de données de capteurs par minute, ainsi que des bases clusterisées gérant plus de 10 millions de données dynamiques. Certifié Microsoft Azure DevOps Engineer Expert, il maîtrise aussi bien le développement back-end (Python, C#) que le DevOps (Docker, Kubernetes, Terraform) et les agents LLM. Formateur en Python, cloud, DevOps et IA générative appliquée, il forme avec une obsession : Amener chaque apprenant à concevoir et déployer des architectures réellement scalables en production.

» Learn More

Associated trainings

All our trainings
Image de la formation Become a Data Analyst
Become a Data Analyst
6 months
Intermediate
Guarantee

Associated articles

See all our articles