File Renamer in Python.
About the project: This program will allow you to rename multiple files in a specified directory based on various criteria.
This Python script provides a simple, menu-driven tool for renaming files. When you run it, you'll be prompted to enter the directory path and then choose an action from the menu.
It provides a menu-driven interface for managing contacts, and all your changes are saved to a file so that they will be there the next time you use the app.
How to use this program:
Run the Python script.
Enter the full path to the directory containing the files you want to rename.
Choose an option from the menu. Currently, you can add a prefix to all files in the directory.
The program will then iterate through the files and rename them accordingly.
You can easily expand this script to include more renaming options, such as replacing parts of a filename, adding suffixes, or using a numbering scheme.
Project Level: Beginner
You can directly copy the below snippet code with the help of green copy button, paste it and run it in any Python editor you have.
Steps: Follow these stepsStep 1: Copy below code using green 'copy' button.
Step 2: Paste the code on your chosen editor.
Step 3: Save the code with filename and .py extention.
Step 4: Run (Press F5 if using python IDLE)
# file_renamer.py
import os
import sys
def rename_files_with_prefix(directory, prefix):
"""
Renames all files in a directory by adding a prefix.
"""
try:
# Get a list of all files in the directory
files = os.listdir(directory)
# Iterate over each file and rename it
for filename in files:
# Construct the new filename with the prefix
new_filename = f"{prefix}_{filename}"
# Construct the full paths
old_path = os.path.join(directory, filename)
new_path = os.path.join(directory, new_filename)
# Rename the file
os.rename(old_path, new_path)
print(f"Renamed: {filename} -> {new_filename}")
print("\nAll files have been renamed successfully.")
except FileNotFoundError:
print(f"Error: The directory '{directory}' was not found.")
except Exception as e:
print(f"An unexpected error occurred: {e}")
def main():
"""
Main function to run the File Renamer with a menu.
"""
print("--- Python File Renamer ---")
# Get the directory from the user
directory_to_rename = input("Enter the path to the directory with files to rename: ").strip()
if not os.path.isdir(directory_to_rename):
print(f"Error: The path '{directory_to_rename}' is not a valid directory.")
sys.exit(1)
while True:
print("\nOptions:")
print("1. Add a prefix to all filenames")
print("2. Quit")
choice = input("Enter your choice (1 or 2): ").strip()
if choice == '1':
prefix = input("Enter the prefix to add: ").strip()
if prefix:
rename_files_with_prefix(directory_to_rename, prefix)
else:
print("Prefix cannot be empty.")
elif choice == '2':
print("Exiting File Renamer. Goodbye!")
break
else:
print("Invalid choice. Please enter 1 or 2.")
# This ensures that main() is called only when the script is executed directly.
if __name__ == "__main__":
main()