Snippets

Rene Cejas Bolecek Rename files in a folder

Created by Rene Cejas Bolecek
#!/usr/bin/env python

##################################################################################
# script function: rename files in a folder
# 
# rel. date: 10/2015
# Dr. Rene Cejas Bolecek
# Low Temperatures Laboratory, CAB, Argentina.
# email: reneczechdev@gmail.com
# licence: MIT. http://opensource.org/licenses/MIT 
##################################################################################

import glob
import shutil

rex = '*.dat*' # Define regular expresion to get the files, not sorted!
files = glob.glob(rex)  

#files.sort()                # sort the list
#print files				 # check sorted list

newFilename = '10_id_'		 # new filename

for f in files:		 # loop over the list
    originalFilename = f                                    
    y = f.split('.dat')  # Reuse some strings from the original filename 
    #print y[1] # just to check
    intVal = int(y[1]) #transform to integer
    shutil.move(f, newFilename+"%d" %(2*intVal+1)+".dat")	 #rename fn 


#Old filename list:
# {150830_iman18_10_20KTo100K_2mmHg.dat0, ..., 150830_iman18_10_20KTo100K_2mmHg.dat40}

#New filename list:
# {10_id_1.dat, ... , 10_id_81.dat}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.