1

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[1]
matchPattern = sys.argv[2]
replacePattern = sys.arg[3]

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?

3
  • If you have access to arcpy (tagged as such) then you can use the Rename tool in the data management toolbox to do this. resources.arcgis.com/en/help/main/10.2/index.html#//…
    – dslamb
    Jun 9, 2016 at 14:41
  • thank you so much dslamb. i don't have access to arcpy (or even arcmap) right now... but i will in the near future. as soon as I do, i'll check out your link. i really appreciate your comment and info - thank you!
    – Geo2036
    Jun 9, 2016 at 16:40
  • I edited your tags to remove arcpy and arcgis desktop since you do not have access to either of these.
    – dslamb
    Jun 9, 2016 at 16:54

2 Answers 2

3

Use the walk function from the data access module and Rename tool from Data management. Refer to the help files for code samples

0
-1

Try this:

import os
import sys
import fnmatch

dirToProcess = sys.argv[1]
matchPattern = sys.argv[2]
replacePattern = sys.argv[3]

print "Processing " + dirToProcess

if (os.path.isdir(dirToProcess)):
    print "dir " + dirToProcess + " found"
else:
    print dirToProcess + " not found"
    exit ()
print "continuing processing..."


for filename in os.listdir(dirToProcess):
    if fnmatch.fnmatch(filename, matchPattern):
        print "found: " + filename + " renaming..."
        filename_base = filename.split('.')[0]
        extension = filename.split(filename_base)[1]
        new_name = (replacePattern + extension ) % filename_base
        os.rename(os.path.join(dirToProcess, filename),
                  os.path.join(dirToProcess, new_name))
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.