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:') ifnot os.path.isdir(path): print path+' is not exist.' else: rename_p(path,file_type)