Introduction

Renaming documents one by one can be tedious and time-consuming, especially when dealing with a large number of files. Fortunately, with Python, you can automate this process and make it faster and less cumbersome. In this tutorial, we will look at different ways to rename files in Python, including renaming files in a specific directory, renaming multiple files at once, and renaming files with a specific pattern. With this knowledge, you can optimize your renaming tasks with Python and save valuable time and effort.

Table of Contents :

  • How to rename a file using rename() method
  • Check whether file exists before renaming
  • Different scenarios of renaming files in python
    • How to rename multiple files in python 
    • Renaming files that matches a pattern
    • How to rename all files in a folder
    • How to rename only a list of files in a folder
    • Renaming files with a timestamp
    • How to rename the extension of the files
  • How to rename an image file
  • How to rename and move a file

How to rename a file using rename() method

  • import the  os  module.
  • Use the  os.rename()  method 
  • The  rename()  method takes two arguments 
    • the original filename and
    • the new filename
  • Code Sample : 

import os
os.rename("old_file_name.txt", "new_file_name.txt")


Check whether file exists before renaming

  • We can use the  os.path.exists()  method to check whether the file exists before renaming.
  • Code Sample : 

import os
if os.path.exists("old_file_name.txt"):
   os.rename("old_file_name.txt", "new_file_name.txt")
   
   
   

Different scenarios of renaming files in python 

How to rename multiple files in python 

  • We can use a for loop to iterate through each file and rename it.
  • Code Sample : 

import os
for file in os.listdir():
   os.rename(file, "new_" + file)
   
   

Renaming files that matches a pattern

  • We can use the  glob  module to get a list of files that match the pattern.
  • We can use a for loop to iterate through each file and rename it.
  • Code Sample : 

import glob
import os

files = glob.glob("*.txt")

for file in files:
   os.rename(file, "new_" + file)
   
   
   

How to rename all files in a folder

  • We can use the   os.listdir()  method to get a list of all files in the folder.
  • We can use a for loop to iterate through each file and rename it.
  • Code Sample : 

import os

dir_path = "/path/to/folder"

for file in os.listdir(dir_path):
   os.rename(os.path.join(dir_path, file), os.path.join(dir_path, "new_" + file))
   
   
   

How to rename only a list of files in a folder

  • Create a list of filenames to rename.
  • We can use a for loop to iterate through each filename and rename it.
  • Code Sample : 

import os

dir_path = "/path/to/folder"

files_to_rename = ["file1.txt", "file2.txt"]

for file in files_to_rename:
   os.rename(os.path.join(dir_path, file), os.path.join(dir_path, "new_" + file))
   
   
   

Renaming files with a timestamp


import os
from datetime import datetime

time_stamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")

os.rename("file.txt", f"file_{time_stamp}.txt")


How to rename the extension of the files

  • Use the  os.path.splitext()  method to separate the filename from the extension.
  • Concatenate the new extension with the filename to form the new name.
  • Code Sample : 

import os

filename, extension = os.path.splitext("file.txt")

os.rename("file.txt", filename + ".docx")


How to rename an image file in python

  • Use the  PIL  module to open the image file.
  • Use the  save()  method with a new filename and extension to save the renamed image.
  • Code Sample : 

from PIL import Image
image = Image.open("image.png")
image.save("new_image.jpg")


How to rename and move a file in python

  • Use the   shutil  module to move the file to a new location.
  • Use the  os.rename()  method to rename the file.
  • Code Sample : 

import shutil
import os

src = "/path/to/folder/file.txt"
dst = "/path/to/new/folder/new_file.txt"
shutil.move(src, dst)

os.rename(dst, "/path/to/new/folder/rename_new_file.txt")


Prev. Tutorial : Python Seek() function

Next Tutorial : Path of a directory