title: Arcpy Script Looping - How do I loop through tables in a folder and perform arcpy join function on each table?
body: <p>I am trying to clean up my script so that I don't have to change the variables each time I want to use it.</p>
  
  <p>I have arcgis tables for each US state (plus DC and Puerto Rico). I want to iterate through those tables in a folder, join each one at a time to a shapefile, copy the joined features to a new featureclass in a different geodatabase, name that featureclass after the respective state name, then remove the join and move on to the next state.</p>
  
  <p>I would consider myself a novice when it comes to python. I've been trying to teach myself for years, but haven't had a good chance to take an in depth class and I don't have anyone I work with that knows it too well. I know there are more efficient ways for scripting like loops, functions, and conditional statements, but I don't know how to set them up correctly.</p>
  
  <p>So I created a script that did what I needed to get done for today, but I'd like to make the script more dynamic. I don't want to have to change each and every table name or new featureclass name. I was trying to look up how to either create a custom function for part of the code, but then also have a loop so it knew to iterate through each table in the folder. I wasn't sure if I need to have the loop first, then the function, or have the loop inside the function. Then I wasn't sure how to get the correct names for the output feature classes, I know theres a way to reference dynamic variable with %s but not sure how to incorporate it here.</p>
  
  <pre><code>layer = arcpy.GetParameterAsText(0)
  inField = "GEOID"
  jTable = r'k:\geospatial\data\census\national\census_fact_finder_data\census_tract_year_built\aa_by_state\xls_pcts\tables'
  jField = "GEOID"
  outFC = r'K:\GEOSPATIAL\DATA\Census\National\Census_Fact_Finder_Data\Shapefiles\CFF_Census_Tracts\PCTs\FCC_CT_YB_PCT.gdb'
  
  arcpy.AddMessage("Processing Arizona...")
  #join table to census tract layer
  arcpy.AddMessage("Joining Arizona table to Census Tracts...")
  tract_join = arcpy.AddJoin_management(layer, inField,jTable + "\\az_pcts", jField, "KEEP_COMMON")
  
  #Copy joined features to new feature class in geodatabase
  arcpy.AddMessage("Exporting joined features to FCC_CT_YB_PCT geodatabase...")
  arcpy.CopyFeatures_management(tract_join, outFC + "\Arizona_PCT")
  
  #remove all joins
  arcpy.AddMessage("Removing joins to process next table...")
  arcpy.RemoveJoin_management(layer)
  arcpy.AddMessage("Arizona Complete")
  </code></pre>
  
  <p>So with this example above, it joined the Arizona table (<code>az_pcts</code>) to the census tracts layer (<code>layer = arcpy.GetParameterAsText(0)</code>), copied the joined features to the new database and named it <code>Arizona_PCT</code>, then removed the join and continued on to the next table. I repeated this same exact structure for each state table and changed all the path endings to what I wanted them to be. If anyone has any advice, even pieces, that would be greatly appreciated.</p>