Это очень просто для всех файлов. Просто замените '*.txt
на *.*
. *.*
означает любое имя файла с любым расширением:
import os, fnmatch
#Set directory of locataion; include double slash for each subfolder.
file_path = "C:\\Users\\Mr.Slowbro\\Desktop\\Source Files\\"
#Set file extension accordingly
files_to_rename = fnmatch.filter(os.listdir(file_path), '*.*') #All files included
for file_name in files_to_rename:
file_name_new = file_name.replace('[', '')
os.rename(file_path + file_name, file_path + file_name_new)
os.rename(file_path + file_name_new, file_path + file_name_new.replace(']', ''))
Для определенных расширений c просто объедините списки:
import os, fnmatch
#Set directory of locataion; include double slash for each subfolder.
file_path = "C:\\Users\\Mr.Slowbro\\Desktop\\Source Files\\"
#Set file extension accordingly
files_to_rename = fnmatch.filter(os.listdir(file_path), '*.txt') + fnmatch.filter(os.listdir(file_path), '*.pdf') + fnmatch.filter(os.listdir(file_path), '*.doc')