Python: Join Table to Shapefile and export it

959
1
02-11-2019 04:25 PM
NirabShrestha
New Contributor

Can anyone help me to iterate through folders which contain number of shapefiles in it and join them to other shapefiles in different folders and later export as a separate file after joining. I tried it doing using model builder, somehow it worked, but is kind of manual and is really time consuming. 

It would be of a great help, if there is a python script for it to automate it.

Thank You.Jake Skinner

0 Kudos
1 Reply
JakeSkinner
Esri Esteemed Contributor

Not sure how you're determining how to join the other shapefile, but if it's based on name you could do something like below:

import arcpy
arcpy.env.workspace = r"C:\data\vector\USA"

shapefileList = []

# Get list of shapefiles
for shapefile in arcpy.ListFeatureClasses("*"):
    shapefileList.append(shapefile)

arcpy.env.workspace = r"C:\data\vector\USA2"
for shapefile in arcpy.ListFeatureClasses("*"):
    for shapefile2 in shapefileList:
        if shapefile == shapefile2:
            # Join shapefile fields if names are the same
            arcpy.JoinField_management(shapefile, "JOINFIELD", shapefile2, "JOINFIELD", ["ID, PIN, ADDRESS"])

You can also use the Join Field tool rather than doing a join and then export.  This will save you a step.

0 Kudos