I'm trying to add "_Kiwi" to the name of multiple shapefiles and raster files in a folder C:\data. For the files with multiple extensions (.shp.xml for example), I can't get the files to rename correctly.
My python script (in c:\data\renamefiles.py) is:
import glob import os import sys dirToProcess = sys.argv matchPattern = sys.argv replacePattern = sys.arg print "Processing " + dirToProcess if (os.path.isdir(dirToProcess)): print dirToProcess + " found" else: print dirToProcess + " not found" exit () print "continuing processing..." def rename (dir, pattern, titlePattern): for pathAndFilename in glob.iglob(os.path.join(dir, pattern)): title, ext = os.path.splitext(os.path.basename(pathAndFilename)) print "found: " + title + "renaming..." os.rename(pathAndFilename, os.path.join(dir, titlePattern % title + ext))
The files with multiple extensions are not getting renamed correctly. Here are two examples:
Example 1: original files is: shapefile1.shp.xml
If I type.... python renamefiles.py c:\data *.shp.xml %s_KIWI, I end up with: shapefile1.shp_KIWI.xml
Example 2: original file is: Raster1.sid.aux.xml
If I type python renamefiles.py c:\data *.sid.aux.xml %s_KIWI, I end up with: Raster1.sid.aux_KIWI.xml
How can I do this with only my current libraries/modules?