python 2.7 - How to rename a file name without changing the extension -


this example code rename file name reference "csv" file.this csv file has old name of file in row[0],and new names in row1 image

example csv file

import csv import os     open('new_names_ddp.csv') csvfile:          reader = csv.reader(csvfile)          row in reader:              oldname = row[0]              newname = row[1]              os.rename(oldname, newname) 

but here have files different extenstions like(.wav,.txt,.xml,.html).so want rename file name new name , add extension automatically.can please me this.

i don't know trying do, because problem , code sample talking else.

i assume want is:

  1. read files in directory hard disk.
  2. if filename (without extension) in column a, rename whatever in column b.

to that:

import os import csv import glob  file_path = '/home/foo/bar/some/where/' file_pattrn = '*.*' file_names = {}  open('somefile.csv') f:     reader = csv.reader(f)     row in reader:        file_names[row[0]] = row[1]  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)))  

Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -