import os
import re
def rename_p(path,file_type):
path=path
filenames=os.listdir(path)
regx="."+file_type+"$"
print regx
for f in filenames:
#print f
if re.search(regx,f)!=None:
'''Split the filename and extension'''
oldname=os.path.splitext(f)[0]
print 'The old name is:', oldname
# ext=os.path.splitext(f)[1] #Get the file extension
# print 'The extentsion is:',ext
new_name=oldname+'new'+'.'+file_type
os.rename(path+'\\'+f,path+'\\'+new_name)
for f in os.listdir(path):
print f
if __name__=='__main__':
#print 'Please input the dir/path which you want to rename the files in'
path=raw_input('Please input the dir/path which you want to rename the files in:')
file_type=raw_input('\nPlease input the file type that you want to rename:')
if not os.path.isdir(path):
print path+' is not exist.'
else:
rename_p(path,file_type)
|