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 intry-exceptor check withos.path.exists(). - Using
os.rmdir()on non-empty folders: This fails if the directory contains any files. Useshutil.rmtree()instead. - Hardcoding paths: Using absolute paths like
"C:/Users/..."makes scripts unportable. Use relative paths orpathlibfor better cross-platform compatibility. - Deleting without confirmation: In interactive scripts, always ask the user before running destructive commands like
rmtree.
When to use it
| Method | Best For | Limitation |
|---|---|---|
os.remove() | Single files | Fails if file doesn't exist |
os.rmdir() | Empty directories | Fails if directory has content |
shutil.rmtree() | Directories with files | Irreversible; 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.