This is my example code to rename a file name with reference with a "csv" file.This csv file has a old name of a file in row[0],and new names in a row1 like this image

import csv
import os
with open('New_Names_DDP.csv') as csvfile:
reader = csv.reader(csvfile)
for row in reader:
oldname = row[0]
newname = row[1]
os.rename(oldname, newname)
But here i have a files with different extenstions like(.wav,.txt,.xml,.html).So I want to rename only file name with new name and add their extension automatically.Can you please help me for this.
I don't know exactly what you are trying to do, because your problem and your code sample are talking about something else.
I assume what you want is:
- Read all the files in some directory from the hard disk.
- If the filename (without extension) is in column a, then rename it to whatever is in column b.
To do that:
import os
import csv
import glob
file_path = '/home/foo/bar/some/where/'
file_pattrn = '*.*'
file_names = {}
with open('somefile.csv') as f:
reader = csv.reader(f)
for row in reader:
file_names[row[0]] = row[1]
for file in glob.iglob(file_path+file_pattrn):
path, filename = os.path.split(file)
filename_noext, ext = os.path.splitext(filename)
new_filename = file_names.get(filename_noext, filename_noext)
os.rename(os.path.join(path, filename),
os.path.join(path, '{}{}'.format(new_filename, ext)))