I'm trying to clean up a list of shapefiles that I've created using GME. I have a series of shapefiles (polylines and polygons) that are named "ID_final_kde.img_LSCV.shp" and "ID_final_kde.img_LSCV_poly.shp", where ID is unique to each individual. I'm trying to rename the polylines to "ID_final_LSCV.shp" and the polygons to "ID_final_LSCV_poly.shp", to no avail.

Here's what I've been trying:

# import modules
import arcpy, os, arcinfo, xlwt,time, glob
from arcpy.sa import *


path = os.getcwd()
arcpy.env.workspace = path + '\\fawn_locations\\FINAL'
arcpy.env.overwriteOutput = True

for each in lines:
    arcpy.Rename_management(each, each[:-16] + 'LSCV')

for each in poly:
    arcpy.Rename_management(each, each[:-20] + 'LSCV_poly')

This code renames the .shp file, but not the associated .cfg, .prj, or .shx files, so I can no longer open the shapefile in ArcMap. How can I loop through the folder, renaming all the files while preserving their extensions?

I'm just learning python.

  • 2
    Instead of string slicing, you could also do a replace, a la each.replace("kde.img_"). That way, you don't need to loop over lines and polygons separately.
    – Paul
    Commented Jan 31, 2017 at 19:01
  • @Paul, can you elaborate on this a bit? I tried for each in folder: each.replace("kde.img_") and each.replace("kde.img_", "") but neither seemed to rename anything.
    – pmcgov
    Commented Jan 31, 2017 at 20:36

2 Answers 2


I don't believe you even need arcpy for this necessarily. I think you just need to change the name of all the shapefiles with that name. So to rename from 'abc' to 'def' you would have abc.shp, abc.cfg, abc.prj, etc. become def.shp, def.cfg, def.prj, etc.

import sys
import os

def renameFiles(path):
    for root, dirs, files in os.walk(path):
        for file in files:
            filename, extension = os.path.splitext(file)
            os.rename(os.path.join(path, file), os.path.join(path, filename[:-20] + 'LSCV_poly' + extension))

  • You are correct that I didn't answer the question, and will edit my answer to do so. However the asker explicitly asks how to loop through the folder and rename all the files.
    – danielm
    Commented Jan 31, 2017 at 19:03
  • Thank you. I had seen something like this, but didn't understand exactly what it did (and didn't know about splitext). Is there a way to restrict the walk to a portion of a folder? I have both the lines and polygons in the same directory.
    – pmcgov
    Commented Jan 31, 2017 at 20:40
  • You could open the workspace in arcpy as you were doing, then use arcpy.Describe(featureClassName) is the path to the .shp for that file. That gives you access to shapeType property where you can check if it is a polygon.
    – danielm
    Commented Jan 31, 2017 at 21:56

Building on @Paul's comment, this is how you can rename your shapefiles. Note that you should make a copy of your data to test this on as the Rename tool has the potential to corrupt your data in multiple ways especially since you have an illegal character . in your name (source).

import arcpy, os

inws = r'C:\temp\inws'

arcpy.env.workspace = inws
shps = arcpy.ListFeatureClasses()

for shp in shps:
    outname = shp.replace("kde.img_", "")
    arcpy.Rename_management(shp, outname)
  • I'm getting an error when I try this. import arcpy, os path = os.getcwd() arcpy.env.workspace = path + '\\fawn_locations\\FINAL' arcpy.env.overwriteOutput = True lines=arcpy.ListFeatureClasses() for each in lines: outname=each.replace("kde.img_", "_") arcpy.Rename_management(each, outname) ExecuteError: Failed to execute. Parameters are not valid. ERROR 000670: output Output Data Element is same as input Input Data Element Failed to execute (Rename).
    – pmcgov
    Commented Feb 1, 2017 at 17:38
  • What is the error?
    – Aaron
    Commented Feb 1, 2017 at 17:38
  • @Aaron- see above
    – pmcgov
    Commented Feb 2, 2017 at 15:07

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.