Given multiple files in a directory having different names, the task is to rename all those files

rename files, replace, find, os, for loop

 

 

# Python progaram to rename all file

# names in your directory
# https://www.geeksforgeeks.org/rename-all-file-names-in-your-directory-using-python/
# https://stackoverflow.com/questions/62668176/how-to-replace-characters-and-rename-multiple-files
import os
import re
 
os.chdir('D:\\chapters')
print(os.getcwd())

for count, f in enumerate(os.listdir()):
    f_name, f_ext = os.path.splitext(f)
    # replace space with ""(remove spaces)
    new_name = f'{f_name.replace(" ", "")}{f_ext}'
    os.rename(f, new_name)