[GIS] Creating a Simple Join Script in Python

arcgis-10.2arcpyattribute-joinserror-000732

I'm trying to create a simple join script in python from an arcgis model, where the only steps are joining a map to a table to produce a map with the table's properties. The attributes joined together are "CNTYFPID" from the shapefile map and "fips" from the textfile. I have tried editing the script produced by arcgis, but it's not working. Where am I going wrong?

# Import arcpy module
import arcpy
arcpy.env.workspace = "C:/Users/Documents/ArcGIS/Testing123.mdb"
arcpy.env.overwriteOutput = True

# Script arguments
Map = arcpy.GetParameterAsText(0)
if Map == 'tl_2009_us_county_albers_USGS.shp' or not Map:
    Map = "C:/Users/Documents/ArcGIS/Python/tl_2009_us_county_albers_USGS.shp" #provide a default value if unspecified

Crosswalk = arcpy.GetParameterAsText(1)
if Crosswalk == '#' or not Crosswalk:
    Crosswalk = "C:\\Users\\Documents\\ArcGIS\\Testing123.mdb\\cao_fips_crosswalk_for_gis" # provide a default value if unspecified)

# Local variables:
Map = "C:\\Users\\Documents\\ArcGIS\\geography\\tl_2009_us_county\\tl_2009_us_county_albers_USGS.shp"
Final = Map

arcpy.MakeTableView_management(Crosswalk, "fips")

#Process: Join Field
arcpy.AddMessage("Performing Join Field")
arcpy.JoinField_management(Map, "CNTYIDFP", Crosswalk, "fips", "")

The python version I am using is 2.7 and the arcgis version is 10.2. This is what it looks like in modelbuilder

Error message: "Traceback (most recent call last): File
"C:/Users/Documents/ArcGIS/Yo2.py", line 28, in
arcpy.MakeTableView_management(Crosswalk, "fips") File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\management.py",
line 6306, in MakeTableView
raise e ExecuteError: Failed to execute. Parameters are not valid. ERROR 000732: Input Table: Dataset
C:\Users\Documents\ArcGIS\Testing123.mdb\cao_fips_crosswalk_for_gis
does not exist or is not supported Failed to execute (MakeTableView)."

Best Answer

I've not used JoinField_management. Every time I want to join I've used AddJoin_management. They appear to do very similar things, so it is possible that the Add Join would work for you.

I am thinking that the problem with your script is that the Join data is of type feature class, not that Python has strong types, but both tools specifically call for layer or table view. The good news is that it's easy to do this.. use MakeFeatureLayer_Management to create a layer.

arcpy.AddMessage("Performing Join Field")
arcpy.MakeFeatureLayer_Management(Map,"Map_Layer")
arcpy.JoinField_management("Map_Layer", "CNTYIDFP", Crosswalk, "fips", "")
Related Question