How to delete multiple files in a directory in Python?

Python provides robust modules for file and directory manipulation, namely the OS and shutil modules. The OS module provides functions for interacting with the operating system, allowing you to perform operations such as creating, removing, and manipulating files and directories.

The shutil module offers a higher-level interface for file operations, making tasks like copying, moving, and deleting entire directories straightforward. Let's explore several methods to delete multiple files in Python.

Using os.remove() to Delete Multiple Files

To delete multiple files, we loop over a list of filenames and apply the os.remove() function for each file ?

import os

# Create sample files for demonstration
for i in range(1, 4):
    with open(f'file{i}.txt', 'w') as f:
        f.write(f'This is file {i}')

# List of files to delete
files_to_delete = ['file1.txt', 'file2.txt', 'file3.txt', 'nonexistent.txt']

# Deleting multiple files
for file_name in files_to_delete:
    try:
        os.remove(file_name)
        print(f'Deleted: {file_name}')
    except FileNotFoundError:
        print(f'File not found: {file_name}')
Deleted: file1.txt
Deleted: file2.txt
Deleted: file3.txt
File not found: nonexistent.txt

Using glob.glob() for Pattern-Based Deletion

The glob module allows you to delete files matching specific patterns, which is useful when dealing with multiple files of the same type ?

import os
import glob

# Create sample files
for i in range(1, 4):
    with open(f'temp{i}.log', 'w') as f:
        f.write(f'Log file {i}')

# Delete all .log files
log_files = glob.glob('*.log')
print(f"Found files: {log_files}")

for file_name in log_files:
    try:
        os.remove(file_name)
        print(f'Deleted: {file_name}')
    except OSError as e:
        print(f'Error deleting {file_name}: {e}')
Found files: ['temp1.log', 'temp2.log', 'temp3.log']
Deleted: temp1.log
Deleted: temp2.log
Deleted: temp3.log

Using os.listdir() with Filter Conditions

You can use os.listdir() to get all files in a directory and apply custom filter conditions to delete specific files ?

import os

# Create sample files
files_data = [('data1.txt', 'small'), ('data2.txt', 'large file content'), ('readme.md', 'info')]
for filename, content in files_data:
    with open(filename, 'w') as f:
        f.write(content)

# Delete .txt files that are smaller than 10 characters
current_dir = '.'
deleted_count = 0

for filename in os.listdir(current_dir):
    if filename.endswith('.txt'):
        filepath = os.path.join(current_dir, filename)
        try:
            file_size = os.path.getsize(filepath)
            if file_size < 10:  # Less than 10 bytes
                os.remove(filepath)
                print(f'Deleted small file: {filename} ({file_size} bytes)')
                deleted_count += 1
        except OSError as e:
            print(f'Error processing {filename}: {e}')

print(f'Total files deleted: {deleted_count}')
Deleted small file: data1.txt (5 bytes)
Total files deleted: 1

Using shutil.rmtree() for Directory Cleanup

When you need to delete an entire directory and all its contents, shutil.rmtree() is the most efficient approach ?

import os
import shutil

# Create a directory with multiple files
os.makedirs('temp_folder', exist_ok=True)
for i in range(3):
    with open(f'temp_folder/file{i}.txt', 'w') as f:
        f.write(f'Content {i}')

print(f"Files in temp_folder: {os.listdir('temp_folder')}")

# Delete the entire directory
try:
    shutil.rmtree('temp_folder')
    print('Directory and all contents deleted successfully')
except OSError as e:
    print(f'Error deleting directory: {e}')
Files in temp_folder: ['file0.txt', 'file1.txt', 'file2.txt']
Directory and all contents deleted successfully

Comparison of Methods

Method Best For Handles Directories? Pattern Matching
os.remove() Single files or file lists No No
glob.glob() Files matching patterns No Yes
os.listdir() Custom filter conditions No Custom
shutil.rmtree() Entire directories Yes No

Conclusion

Use glob.glob() for pattern-based file deletion, os.remove() with loops for specific file lists, and shutil.rmtree() for removing entire directories. Always use try-except blocks to handle file not found errors gracefully.

Updated on: 2026-03-24T17:22:23+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements