Back to Python Notes
Topic #232

Delete Files

By the end of this lesson, you will be able to safely delete files and empty directories in Python using the standard library.

What it is

Deleting files and folders is a fundamental file system operation. In Python, this is primarily handled by the os module for basic tasks and the shutil module for more complex directory removals. The mental model is simple: you provide a path to the target, and the operating system removes the entry from the directory structure. Note that deleting a folder usually requires it to be empty first, unless you use recursive deletion tools.

Why it matters

  • Cleanup: Removing temporary files generated during data processing or testing prevents disk space bloat.
  • Automation: Scripts can manage log rotation or clear old cache directories automatically.
  • Data Management: Ensuring only relevant files remain in a directory simplifies subsequent analysis.
  • Error Handling: Properly managing missing files prevents scripts from crashing unexpectedly.

Syntax or steps

To delete a single file, use os.remove(path). To delete an empty directory, use os.rmdir(path). If you need to delete a directory containing files, use shutil.rmtree(path). Always check if the path exists before attempting deletion to avoid FileNotFoundError.

Example

import os
import shutil

# 1. Delete a specific file
file_path = "temp_data.txt"
if os.path.exists(file_path):
    try:
        os.remove(file_path)
        print(f"Deleted file: {file_path}")
    except OSError as e:
        print(f"Error deleting file: {e}")
else:
    print("File not found.")

# 2. Delete an empty directory
dir_path = "empty_folder"
if os.path.isdir(dir_path):
    try:
        os.rmdir(dir_path)
        print(f"Deleted empty directory: {dir_path}")
    except OSError as e:
        print(f"Error deleting directory: {e}")

# 3. Recursively delete a non-empty directory
non_empty_dir = "project_cache"
if os.path.isdir(non_empty_dir):
    shutil.rmtree(non_empty_dir)
    print(f"Recursively deleted: {non_empty_dir}")

This code first checks existence to prevent errors. It uses try-except blocks to catch permission issues or race conditions. Finally, it demonstrates shutil.rmtree for removing directories with contents.

Common mistakes

  • Ignoring exceptions: Calling os.remove() on a non-existent file raises an error. Always wrap calls in try-except or check with os.path.exists().
  • Using os.rmdir() on non-empty folders: This fails if the directory contains any files. Use shutil.rmtree() instead.
  • Hardcoding paths: Using absolute paths like "C:/Users/..." makes scripts unportable. Use relative paths or pathlib for better cross-platform compatibility.
  • Deleting without confirmation: In interactive scripts, always ask the user before running destructive commands like rmtree.

When to use it

MethodBest ForLimitation
os.remove()Single filesFails if file doesn't exist
os.rmdir()Empty directoriesFails if directory has content
shutil.rmtree()Directories with filesIrreversible; no built-in undo

Practice

Guided Exercise: Create a script that deletes all .log files in the current directory. Hint: Use glob.glob("*.log") to find files, then loop through them calling os.remove().

Challenge: Write a function that safely deletes a directory tree but moves it to a "trash" folder first instead of permanently deleting it. Hint: Use shutil.move() before shutil.rmtree().

Quick check

Q: What happens if you call os.rmdir("my_folder") when "my_folder" contains a file named "data.csv"?

A: It raises an OSError (specifically [Errno 39] Directory not empty). You must remove the contents first or use shutil.rmtree().

Summary

Python provides distinct tools for different deletion scenarios: os.remove for files, os.rmdir for empty dirs, and shutil.rmtree for recursive cleanup. Always validate paths and handle exceptions to ensure robust file management scripts.

Want to go beyond the notes?

Join Coding Now Tech Institute's Python course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Delete Files – FAQs

Quick answers about learning Delete Files in Python.

This free note from Coding Now Tech Institute explains Delete Files in Python — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Python topic on Coding Now Tech Institute, including Delete Files, is 100% free with no signup required.
With focused practice, most students grasp Delete Files in 1–3 days from these notes; pairing it with Coding Now Tech Institute's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now