Select to view content in your preferred language

Loop through and Rebuild Locators

963
2
12-05-2017 01:30 PM
StevenGraf1
Frequent Contributor

I'd like to loop through a file folder and rebuild all the locators in it.  Unfortunately, I can't find anything that returns the locator name.  I have been able to loop through using arcpy.ListFiles and find all *.loc files but arcpy.RebuildAddressLocator_geocoding doesn't allow you to use this because of the .loc.  It says it cannot open the locator.  The Esri technical article doesn't apply here because I can rebuild it fine as long as I don't include the .loc.

Throws the error 000005 Could not open address locator

# import system modules
import os
import arcpy

# Set environment settings
arcpy.env.workspace = "C:/Locators/Test_LocatorRebuild/"

for loc in arcpy.ListFiles("*.loc"):
    print(loc)
    arcpy.RebuildAddressLocator_geocoding(loc)
‍‍‍‍‍‍‍‍‍‍

Anyone have any suggestions.

I am currently just adding all locators into the python array and looping through them that way.  It's not ideal but works for now.

# import system modules
import os
import arcpy

#Set my_workspace to the folder location of the locators
my_workspace = r'C:\Locators\Test_LocatorRebuild\\'

#Set my_locators array to include the names of each locator to be rebuilt
my_locators = [my_workspace + "Streets_Test",
               my_workspace + "Structures_Test"]

#Loop through the array and rebuild the address locators
for loc in my_locators:
    print("Rebuilding " + loc)
    arcpy.RebuildAddressLocator_geocoding(loc)
    print(loc + " rebuild complete")
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
2 Replies
StevenGraf1
Frequent Contributor

I had to strip the extension and re-add the file path for it to work.  Final script is below.

# import system modules
import os
import arcpy

arcpy.env.workspace = r"C:\Locators\Test_LocatorRebuild\\"

for loc in arcpy.ListFiles("*.loc"):
    locator = os.path.splitext(os.path.basename(loc))[0]
    print("Rebuilding " + locator)
    arcpy.RebuildAddressLocator_geocoding(arcpy.env.workspace + "\\" + locator)
DanPatterson_Retired
MVP Emeritus

Be careful about adding backslashes to the end of paths... in some cases they don't work

locator = os.path.splitext(os.path.basename(in_fc))[0]

a = r"C:\Locators\Test_LocatorRebuild\\" + "\\" + locator

print(a)

C:\Locators\Test_LocatorRebuild\\\Polygon
0 Kudos