In Python file handling, you are not only able to create, read, and write files, but also rename and delete files when they are no longer needed or when you want to organize your data better.
Python provides the built-in os module to perform these operations easily.
In this tutorial, you will learn how to rename and delete files in Python with clear examples.
What is File Renaming and Deleting?
- Renaming a file means changing its name without changing its content.
- Deleting a file means permanently removing it from storage.
Importing the OS Module
To work with files, we use the os module.
import os1. Renaming a File
The os.rename() function is used to rename files.
Syntax:
os.rename("old_name.txt", "new_name.txt")Example:
import os
os.rename("example.txt", "newfile.txt")
print("File renamed successfully")Explanation:
example.txt→ old file namenewfile.txt→ new file name- File content remains unchanged
2. Checking File Before Renaming
Always check if file exists before renaming.
Example:
import os
if os.path.exists("example.txt"):
os.rename("example.txt", "newfile.txt")
print("File renamed successfully")
else:
print("File does not exist")3. Deleting a File
The os.remove() function is used to delete files.
Example:
import os
os.remove("newfile.txt")
print("File deleted successfully")4. Checking File Before Deleting
It is important to check if the file exists before deleting.
Example:
import os
if os.path.exists("newfile.txt"):
os.remove("newfile.txt")
print("File deleted successfully")
else:
print("File not found")5. Delete File Using Try-Except
You can also handle errors safely.
Example:
import os
try:
os.remove("newfile.txt")
print("File deleted successfully")
except FileNotFoundError:
print("File does not exist")6. Renaming Multiple Files (Example Concept)
You can rename multiple files using a loop.
Example:
import os
files = ["file1.txt", "file2.txt", "file3.txt"]
for i, file in enumerate(files):
new_name = f"new_file_{i}.txt"
os.rename(file, new_name)Real-World Example: Log File Management
Rename old log file:
import os
if os.path.exists("log.txt"):
os.rename("log.txt", "log_old.txt")Real-World Example: Clean Temporary Files
import os
temp_files = ["temp1.txt", "temp2.txt"]
for file in temp_files:
if os.path.exists(file):
os.remove(file)
print(file, "deleted")Difference Between Rename and Delete
| Operation | Function | Result |
|---|---|---|
| Rename | os.rename() | Changes file name |
| Delete | os.remove() | Removes file permanently |
Common Mistakes
Mistake 1: File does not exist
❌ Wrong:
os.rename("old.txt", "new.txt")✔ Causes FileNotFoundError
Mistake 2: Wrong file path
os.remove("wrongfile.txt")✔ File not found error
Mistake 3: Accidentally deleting important file
✔ Always double-check file name before deletion
Safe File Handling Example
import os
file_name = "data.txt"
if os.path.exists(file_name):
os.remove(file_name)
print("Deleted safely")
else:
print("File not found")File Management Summary
| Task | Function |
| Rename file | os.rename() |
| Delete file | os.remove() |
| Check file | os.path.exists() |
| Error handling | try-except |
Conclusion
Renaming and deleting files in Python is essential for managing system files and organizing data.
You learned:
- How to rename files using
os.rename() - How to delete files using
os.remove() - How to safely check files before operations
- Real-world use cases like log management and cleanup
These operations are widely used in automation, system scripting, and file management applications.


0 Comments