Python Script to Bulk Rename Files

Renaming hundreds of files manually is painful. With a few lines of Python, you can batch rename files in seconds.

import os

folder = "my_files"
for count, filename in enumerate(os.listdir(folder), start=1):
    ext = os.path.splitext(filename)[1]
    new_name = f"file_{count}{ext}"
    os.rename(os.path.join(folder, filename),
              os.path.join(folder, new_name))

  • Loop through all files in the target folder.
  • Extract the file extension.
  • Rename each file sequentially (file_1.jpg, file_2.jpg, …).

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *