Python Script to Rename Files in Seconds

Python Script to Rename Files in Seconds (Complete Beginner Guide)

Renaming files manually is slow, frustrating, and unnecessary—especially when working with many files.

With Python, you can automate the entire process in seconds. In this guide, you’ll learn how to use a Python script to rename files, understand how it works, and avoid common mistakes.

Quick Insight: A simple Python script can rename hundreds of files instantly with zero manual effort.

Why Use Python to Rename Files?

Manual renaming becomes inefficient very quickly. Python solves this by automating repetitive tasks.

  • ✔ Rename hundreds of files in seconds
  • ✔ Avoid human errors
  • ✔ Maintain consistent naming
  • ✔ Save time and effort

What You Need Before Starting

  • Python installed on your computer
  • A folder containing files to rename
  • Basic knowledge of running Python scripts

Complete Python Script to Rename Files (Safe Version)

import os

def rename_files():
folder_path = input("Enter the full folder path: ").strip()

```
if not os.path.exists(folder_path):
    print("Error: Folder does not exist.")
    return

files = os.listdir(folder_path)
files = [f for f in files if os.path.isfile(os.path.join(folder_path, f))]

if not files:
    print("No files found in the folder.")
    return

print(f"Found {len(files)} files.")

prefix = input("Enter new file name prefix: ").strip()

for count, filename in enumerate(files, start=1):
    old_path = os.path.join(folder_path, filename)
    extension = os.path.splitext(filename)[1]
    new_name = f"{prefix}_{count}{extension}"
    new_path = os.path.join(folder_path, new_name)

    if os.path.exists(new_path):
        print(f"Skipping {filename} (file already exists)")
        continue

    try:
        os.rename(old_path, new_path)
        print(f"Renamed: {filename} → {new_name}")
    except Exception as e:
        print(f"Error renaming {filename}: {e}")

print("File renaming completed!")
```

if **name** == "**main**":
rename_files()

This script safely renames files by preventing errors, skipping duplicates, and allowing user input.

How This Script Works

  • input() → gets the folder path and file prefix from the user
  • os.listdir() → retrieves all files in the folder
  • os.path.isfile() → ensures only files are processed
  • os.path.splitext() → keeps file extensions intact
  • os.rename() → renames each file
Pro Tip: Always test your script on a small folder before running it on important files.

Common Mistakes to Avoid

  • ❌ Using an incorrect folder path
  • ❌ Overwriting important files
  • ❌ Forgetting to test first
  • ❌ Renaming system or hidden files

Real-Life Use Cases

  • Organizing downloaded files
  • Renaming images in bulk
  • Cleaning messy folders
  • Preparing files for upload

Frequently Asked Questions (FAQ)

Can this script rename folders too?

Yes, but you need to modify the script to target directories instead of files.

Will this delete my files?

No, it only renames them. However, incorrect usage can overwrite files if not handled carefully.

Do I need internet to run this?

No, Python runs locally on your computer.

Final Thoughts

Using a Python script to rename files is one of the easiest ways to automate repetitive tasks and save time.

Once you understand this, you can expand into more advanced automation projects.

Next Step: Try this script on a test folder and see the results instantly.

Comments