LinearAndLoom Icons

Remove Numbers From the End of File Names (Free Script)

Free Python script that strips trailing numbers such as _01, -2 or (space)3 from file names in a folder and all its subfolders.

Download remove-trailing-numbers-from-filenames.pyPython 3 · 75 lines · 3 KB · free

Exports, downloads and copies often get a number stuck on the end: logo_01.svg, banner-2.png, report 3.pdf. Removing them by hand across many folders is slow. This script does it for a whole folder tree at once.

What it does

Numbers in the middle of a name, as in icon-24px-outline.svg, are not touched.

How to use it

  1. Run the script (see below). A folder window opens.
  2. Choose the folder to clean.
  3. When it finishes, a message shows how many files were renamed and asks whether you have another folder. Click Yes to pick the next one or No to stop.

Renaming cannot be undone with Ctrl+Z. Run the script on a copy of the folder the first time, until you trust the result.

How to run a Python script

  1. Install Python 3 from python.org if you do not have it. It is free. On Windows, tick Add Python to PATH in the installer.
  2. Download the script with the button on this page and save it somewhere easy, such as your Desktop.
  3. Open a terminal. On Windows, press the Windows key, type cmd and press Enter. On Mac, open Terminal from Applications > Utilities.
  4. Type python followed by a space (on Mac, python3 and a space), drag the script file into the terminal window and press Enter.

On Windows you can also just double-click the script after installing Python.

The folder window is drawn with Tkinter, which comes with Python on Windows and Mac. On Linux, install it with sudo apt install python3-tk.

Good to know

If a folder holds photo_1.jpg and photo_2.jpg, only the first can become photo.jpg. The second is skipped and reported in the terminal window, and keeps its number.

Questions

Will it remove the number from "version2-final.png"?

No. Only numbers at the very end of the name, just before the extension, are removed.

Does it change folder names?

No, only file names.

Can I remove numbers from the start of names instead?

Not with this script as it is. In the script, the pattern [\s\-_]*\d+$ matches numbers at the end. Changing it to ^\d+[\s\-_]* matches numbers at the start.

The code

This is the complete script. Nothing else is downloaded or installed.

"""Remove trailing numbers from file names

Strips the numbers at the end of file names in a folder and all its subfolders:
"logo-design_01.svg" and "logo-design 1.svg" become "logo-design.svg".

Free script from LinearAndLoom Icons: https://linearandloomicons.netlify.app/scripts/
Provided as is. Renaming and zipping cannot be undone, so try it on a copy of your folder first.
"""
import os
import re
import tkinter as tk
from tkinter import filedialog, messagebox

def remove_trailing_numbers():
    # Hide the main root tkinter window
    root = tk.Tk()
    root.withdraw()
    
    print("Script started. Follow the popups...")

    while True:
        # Open folder selection dialog
        folder_path = filedialog.askdirectory(title="Select Folder to Rename Files")
        
        # If user cancels or closes the dialog, exit the loop
        if not folder_path:
            print("No folder selected. Exiting script.")
            break
            
        renamed_count = 0
        
        # 1. Use os.walk with topdown=False to safely recurse into all subfolders
        for dirpath, dirnames, filenames in os.walk(folder_path, topdown=False):
            
            # 2. Iterate through files in the current subfolder
            for filename in filenames:
                file_path = os.path.join(dirpath, filename)
                
                name, ext = os.path.splitext(filename)
                
                # Regex to match ALL trailing numbers (\d+) and preceding spaces/dashes/underscores
                new_name = re.sub(r'[\s\-_]*\d+$', '', name)
                
                # A name made only of numbers ("2024.jpg") would end up empty, so leave it
                if not new_name:
                    print(f"Skipped: '{filename}' has no name left without its numbers.")
                    continue

                # If a new name is generated, rename the file
                if new_name != name:
                    new_filename = new_name + ext
                    new_file_path = os.path.join(dirpath, new_filename)
                    
                    # Handle potential duplicate destination filenames
                    if os.path.exists(new_file_path):
                        print(f"Skipped: '{new_file_path}' already exists.")
                        continue
                        
                    os.rename(file_path, new_file_path)
                    renamed_count += 1
        
        # Explicit confirmation prompt asking if you want to keep going
        keep_going = messagebox.askyesno(
            "Process Complete", 
            f"Renamed {renamed_count} files across this folder and its subfolders.\n\nDo you have more folders to process?"
        )
        
        # If the user clicks 'No', break the loop and end the script
        if not keep_going:
            print("Finished processing. Exiting script.")
            break

if __name__ == "__main__":
    remove_trailing_numbers()

Download remove-trailing-numbers-from-filenames.pyPython 3 · 75 lines · 3 KB · free

These scripts are free. If one saved you time, you can say thanks by buying a pack from the LinearAndLoom Etsy shop.

More scripts