Rename a list of SHP files in Python

6886
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
IanMurray
Frequent Contributor

Looking at the results, its literally only changing the name of the .shp files, and not some of the other file components that comprise a working shapefile.  Notice your dbf file and prj file are still the old file name when shown in catalog.  You could do a arcpy listfiles with wildcards for those file endings and change the names similar to what you did for the .shp files.  I would also check the other elements of your shapefile in windows (.shx, .sbx, .sbn, etc.) and make sure they are properly renamed as well.

FranciscoSobrado_Llompart
New Contributor II

Hello Ian,

I have tried with the following code and it seams to work...But:

import arcpy, os

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

for filename in arcpy.ListFiles():

    print filename

    os.rename(os.path.join(arcpy.env.workspace, filename), os.path.join(arcpy.env.workspace, filename.replace('.', '_')))

    print "ok"

EX22_28_MA_W_dbf

ok

EX22_28_MA_W_prj

ok

EX22_28_MA_W_sbn

ok

Continuous...

But when I am trying to open the SHP file in ArcMap the files are not there, although the folder contains the files as displayed by the python script

Any ideas...

Thanks

0 Kudos
WesMiller
Regular Contributor III

Try using the Rename (Data Management) tool.

JoshuaBixby
MVP Esteemed Contributor

Your stripping the file extensions off of the files, so ArcGIS Desktop won't see them anymore as shapefiles because the files don't have the proper file extensions.

Although I am a big fan of using native Python tools instead of ArcPy tools, sometimes the ArcPy tools work best.  In this case, I recommend using the Rename tool because it will take care of renaming all the associated/related shapefile files along with the SHP file.

FranciscoSobrado_Llompart
New Contributor II

Hello Joshua,

I have use rename with the following code but it does not work:

import arcpy, os

try:

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

    for filename in arcpy.ListFiles():

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

        a="shp_"+a

        arcpy.Rename_management(filename, a)

        print "ok"

except:

    print arcpy.GetMessages()

I get the following error message:

Executing: Rename EX22.28_MA_W.shp.xml G:\WWF\0_GeoDatabase_GIS\6_Especies\Conejo\Seguimiento\Cuadriculas\WWF\Andujar\2012\RAMON_1\shp_EX22_28_MA_W_shp_xml File

Start Time: Tue Sep 08 16:25:27 2015

Failed to execute. Parameters are not valid.

ERROR 000732: Input Data Element: Dataset EX22.28_MA_W.shp.xml does not exist or is not supported

Failed to execute (Rename).

Failed at Tue Sep 08 16:25:27 2015 (Elapsed Time: 0,01 seconds)

Any idea...

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Try this code, see if:  1) it works, and 2) it gives the results you want:

import arcpy, os

arcpy.env.workspace = r'D:\transfer\geodata'

for shp in arcpy.ListFiles('*.shp'):
    root, ext = os.path.splitext(shp)
    new_root = root.replace('.','_')
    if new_root != root:
        arcpy.Rename_management(shp, new_root + ext)
DanPatterson_Retired
MVP Emeritus

You have to replace the filename not the extension...see the example

>>> file_bits = ["a.shp","a.dbf","a.shx"]
>>> add_to = "_stuff."
>>> for i in file_bits:
...  bits = i.split(".")
...  newName= bits[0] + add_to + bits[1]
...  print newName
... 
a_stuff.shp
a_stuff.dbf
a_stuff.shx
>>>
FranciscoSobrado_Llompart
New Contributor II

Thanks Dan,

But in my case Have "." in the middle of the name... see example below

file_bits = ["a.b.b.shp","a.b.dbf","a.b.b.shx"

if I use split("."), how can I replace only the file name and not the extension...

0 Kudos
BlakeTerhune
MVP Regular Contributor

You've discovered the reason why it's not a good practice to use special (reserved) characters as part of a file name. Just because you can, doesn't mean you should.

23087 - What characters should not be used in ArcGIS for field names and table names?

For ArcGIS to work with multiple data types, certain characters in field or table names are not supported...

  • Eliminate any characters that are not alphanumeric character or an underscore.
  • Do not start field or table names with an underscore or a number.