How to batch rename folders and files in Python

Add characters to the end of all file and folder names

import os
dir_path = "./dirname"
dir_list = os.listdir(dir_path)
for i in range(len(dir_list)):
    new_file_name = dir_list[i] + "!!!"
    os.rename(os.path.join(dir_path,dir_list[i]),os.path.join(dir_path,new_file_name))

How to modify or remove only certain characters from file and folder names

import os
import glob

before_word = "!!!"
after_word = ""

files = glob.glob('*'+ before_word +'*')
for before_file_name in files:
    after_file_name = before_file_name.replace(before_word,after_word)
    os.rename(before_file_name,after_file_name)

Comments

Copied title and URL