Rename a list of SHP files in Python

6826
11
09-08-2015 06:32 AM
FranciscoSobrado_Llompart
New Contributor II

Hello people,

I have a list of SHP files that I want to rename. First, I want to replace the "." from the shp to "_", and then rename the file. As you can see in the image below:

This the code that I am using:

import arcpy, os

arcpy.env.workspace=r'H:\WWF\0_GeoDatabase_GIS\6_Especies\Conejo\Seguimiento\Cuadriculas\WWF\Andujar\2012\RAMON_1'

for filename in arcpy.ListFeatureClasses():

  

    a=filename.replace('.', '_')

    a="shp_"+a

    arcpy.Rename_management(filename, a)

   

    print "ok"

The problem is that when I am trying to open the SHP file it does not work. I am getting the following error message:

Does anyone knows how to fix the problem...

Thanks for your help

0 Kudos
11 Replies
FranciscoSobrado_Llompart
New Contributor II

The code seems to work... it does not give any error message in PyScripter...But when I am trying to add the shp to ArcMAP it gives the following error:

0 Kudos
WesMiller
Regular Contributor III

Your trying to hard

import arcpy
arcpy.env.worcspace = "Your workspace"
for file in arcpy.ListFeatureClasses():
  a = "shp_" + file.replace('.','_')
  arcpy.Rename_management(file,a)