I have multiple shapefiles, in different sources (D:\GIS\Folder A", "D:\GIS_Temp\Name". And some of them have space in their name (File X, File Y, etc).

What should be the code that could rename all shapefiles in those folders, and change space with underscore, so the name of shapefiles would be File_X, File_Y, and so on...

When I try with this code:

import os, sys, arcpy
InWork =  ["D:\\GIS_Temp\Folder A", "D:\\GIS_Temp\\Folder B", "D:\\GIS_Temp\\Folder C"]
arcpy.env.workspace = InWork
for thisFC in arcpy.ListFeatureClasses():
newName = thisFC.replace(" ","_")

I put the folders in InWork, and I still get an error.

Runtime error Traceback (most recent call last): File "", line 3, in File "c:\program files\arcgis\desktop10.1\arcpy\arcpy\geoprocessing_base.py", line 515, in set_ self[env] = val File "c:\program files\arcgis\desktop10.1\arcpy\arcpy\geoprocessing_base.py", line 567, in setitem ret_ = setattr(self._gp, item, value) RuntimeError: Object: Error in accessing environment

What am I doing wrong?

2 Answers 2


The following would do it with your directories:

import arcpy

InWork =  ["D:\\GIS_Temp\Folder A", "D:\\GIS_Temp\\Folder"]

for ws in InWork:
    arcpy.env.workspace = ws
    datasets = arcpy.ListFeatureClasses()
    for fc in datasets:
        newName = fc.replace(' ','_')
        arcpy.Rename_management(fc, newName[:-4])

Note the [:-4], which I do to strip the .shp from the original name. Otherwise Python would name it, for example, dataset.shp.shp and that would crash ArcPy.

  • 1
    This code seems to be doing the job. I have been missing the last line to do the job i needed.
    – Dean7
    Apr 27, 2016 at 19:14
  • Yes, and remember that you cannot set a workspace to a list of directories. ArcPy can only be in one workspace at a time. Therefore the for loop, which first renames everything in one workspace, then the next one, and so on. Apr 28, 2016 at 6:40
  • I'll keep that in mind. If you hadn't point that out, I would probably make a mistake.
    – Dean7
    Apr 28, 2016 at 17:53

I would use Rename_management to make sure you get all the parts to the shape file:

import os, sys, arcpy

InWork = sys.argv[1] # this script designed to be used as a tool, replace with a path if you wish
arcpy.env.workspace = InWork

for thisFC in arcpy.ListFeatureClasses():
    newName = thisFC.replace(" ","_")

Note the .replace() does the main work and can be called many times to change any characters (commas, dashes etc). Note for the purposes of your previous question you must also get rid of starting underscores and numbers, after the first character they're ok but you can't start with a number or underscore.

To make this faster you could consider only renaming if changed:

if thisFC != newName:
  • I will fist rename and change starting dashes and numbers.
    – Dean7
    Apr 27, 2016 at 3:28
  • I tried to use your example, but it does not work. Can you please add more detailed example, with path names, etc.
    – Dean7
    Apr 27, 2016 at 4:03
  • change InWork = sys.argv[1] to InWork = "c:\\your\\path", as I said it's taken from one of my scripts that's used as a tool have a read of resources.arcgis.com/en/help/main/10.1/index.html#//… for more information on script tools and how to use them in the toolbox. Apr 27, 2016 at 21:40
  • I works now. I will have also a detailed read of this link. Thanks!
    – Dean7
    Apr 27, 2016 at 21:52

Your Answer

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

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