# Import arcpy module import arcpy arcpy.env.workspace = "G:\ChrisGIS\WDFW_NWIFC_EVENT_17100101.gdb" arcpy.env.overwriteOutput = "True" def GetFieldMappings(inputFC, targetFC, dico): field_mappings = arcpy.FieldMappings() field_mappings.addTable(inputFC) for input, output in dico.iteritems(): field_map = arcpy.FieldMap() field_map.addInputField(inputFC, input) field = field_map.outputField field.name = output field_map.outputField = field field_mappings.addFieldMap(field_map) del field, field_map return field_mappings #Variables inputFC = "WDFW_CHFA_2HEM_evt_1" targetFC = "WDFW_CHFA_2HEM_evt_olay_1" dico = {'Permanent_Identifier': 'Permanent_ID', 'STRM_NAME': 'LLID_STRM_NAME', 'DATE_SRVY': 'SOURCE_DATE', 'SPPCODE': 'SPECRCODE', 'SRC_AGENCY': 'SOURCE_AGENCY', 'SRC_WHO': 'SOURCE_WHO'} Mapper = GetFieldMappings(inputFC, targetFC, dico) # Process: Append arcpy.Append_management(arcpy.env.workspace+"\\"+inputFC, arcpy.env.workspace+"\\"+targetFC,"NO_TEST",Mapper)
Solved! Go to Solution.
arcpy.env.workspace = r'some\path' for ws in arcpy.ListWorkspaces("*", "FileGDB"): arcpy.env.workspace = ws for fc in arcpy.ListFeatureClasses('WDFW*'): # paste your code here # do stuff
WDFW_CHFA_2HEM_evt_1-------WDFW_CHFA_2HEM_evt_olay_1
WDFW_CCT_2HEM_evt_1--------WDFW_CCT_2HEM_evt_olay_1
WDFW_CHMF_2HEM_evt_1-------WDFW_CHMF_2HEM_evt_olay_1
WDFW_CHSP_2HEM_evt_1-------WDFW_CHSP_2HEM_evt_olay_1
WDFW_COHO_2HEM_evt_1-------WDFW_COHO_2HEM_evt_olay_1
WDFW_KOK_2HEM_evt_1---------WDFW_KOK_2HEM_evt_olay_1
The long way would be to change my workspace each time to the correct GDB then run my script for each InputFC. The list above for example would have me run the script 6 times. Each time I would edit the variables : inputFC and targetFC.
Trying to automate this is proving a nightmare!
# Import arcpy module
import arcpy
arcpy.env.overwriteOutput = True
from os import path as p
def GetFieldMappings(inputFC, targetFC, dico):
field_mappings = arcpy.FieldMappings()
field_mappings.addTable(inputFC)
for input, output in dico.iteritems():
field_map = arcpy.FieldMap()
field_map.addInputField(inputFC, input)
field = field_map.outputField
field.name = output
field_map.outputField = field
field_mappings.addFieldMap(field_map)
del field, field_map
return field_mappings
#Variables
dico = {'Permanent_Identifier': 'Permanent_ID',
'STRM_NAME': 'LLID_STRM_NAME',
'DATE_SRVY': 'SOURCE_DATE',
'SPPCODE': 'SPECRCODE',
'SRC_AGENCY': 'SOURCE_AGENCY',
'SRC_WHO': 'SOURCE_WHO'}
arcpy.env.workspace = r"G:\ChrisGIS"
for ws in arcpy.ListWorkspaces("*", "FileGDB"):
arcpy.env.workspace = ws
for fc in arcpy.ListFeatureClasses():
if fc[-6:] != 'olay_1':
inputFC = fc
targetFC = fc[:-1] + 'olay_1'
Mapper = GetFieldMappings(inputFC, targetFC, dico)
# Process: Append
arcpy.Append_management(p.join(ws,inputFC), p.join(ws,targetFC),"NO_TEST",Mapper)
print 'Appended "{0}" to "{1}" in {2}'.format(inputFC, targetFC, p.basename(ws))
# Import arcpy module import arcpy arcpy.env.overwriteOutput = True from os import path as p def GetFieldMappings(inputFC, targetFC, dico): field_mappings = arcpy.FieldMappings() field_mappings.addTable(inputFC) for input, output in dico.iteritems(): field_map = arcpy.FieldMap() field_map.addInputField(inputFC, input) field = field_map.outputField field.name = output field_map.outputField = field field_mappings.addFieldMap(field_map) del field, field_map return field_mappings #Variables dico = {'Permanent_Identifier': 'Permanent_ID', 'STRM_NAME': 'LLID_STRM_NAME', 'DATE_SRVY': 'SOURCE_DATE', 'SPPCODE': 'SPECRCODE', 'SRC_AGENCY': 'SOURCE_AGENCY', 'SRC_WHO': 'SOURCE_WHO'} arcpy.env.workspace = r"G:\ChrisGIS\COMBO_fishdist_events" for ws in arcpy.ListWorkspaces("*", "FileGDB"): listFCS = arcpy.ListTables("WDFW*evt") arcpy.env.workspace = ws for inputFC in listFCS: targetFC = inputFC +"_olay" Mapper = GetFieldMappings(inputFC, targetFC, dico) # Process: Append arcpy.Append_management(p.join(ws,inputFC), p.join(ws,targetFC),"NO_TEST",Mapper) print 'Appended "{0}" to "{1}" in {2}'.format(inputFC, targetFC, p.basename(ws))
"Runtime error <class 'arcgisscripting.ExecuteError'>: Failed to execute. Parameters are not valid. ERROR 000732: Target Dataset: Dataset G:\ChrisGIS\COMBO_fishdist_events\WDFW_NWIFC_EVENT_17100101.gdb\WDFW_CCT_2HEM_evt_olay does not exist or is not supported Failed to execute (Append)."
# Import arcpy module import arcpy arcpy.env.overwriteOutput = True from os import path as p def GetFieldMappings(inputFC, targetFC, dico): field_mappings = arcpy.FieldMappings() field_mappings.addTable(inputFC) for input, output in dico.iteritems(): field_map = arcpy.FieldMap() field_map.addInputField(inputFC, input) field = field_map.outputField field.name = output field_map.outputField = field field_mappings.addFieldMap(field_map) del field, field_map return field_mappings #Variables dico = {'Permanent_Identifier': 'Permanent_ID', 'STRM_NAME': 'LLID_STRM_NAME', 'DATE_SRVY': 'SOURCE_DATE', 'SPPCODE': 'SPECRCODE', 'SRC_AGENCY': 'SOURCE_AGENCY', 'SRC_WHO': 'SOURCE_WHO'} arcpy.env.workspace = r"G:\ChrisGIS\COMBO_fishdist_events" for ws in arcpy.ListWorkspaces("*", "FileGDB"): arcpy.env.workspace = ws listFCS = arcpy.ListTables("WDFW*evt") for inputFC in listFCS: targetFC = inputFC +"_olay" Mapper = GetFieldMappings(inputFC, targetFC, dico) # Process: Append arcpy.Append_management(p.join(ws,inputFC), p.join(ws,targetFC),"NO_TEST",Mapper) print 'Appended "{0}" to "{1}" in {2}'.format(inputFC, targetFC, p.basename(ws))
Thanks for the quick replies Caleb. When I try to run the last script you provided I keep getting a syntax error on line 34 which reads
if inputFC[-5:] != "_olay"
what is the error?
Do I have to exclude the olay files? I do not have any olays in my geodatabases to start, after running the script there will be tables name ***_olay. I thought when I make my FeatureClass.list tables the append will only be run on the values found in that list. If this is the case there are no _olay tables, as they are only created when I run my script. It may seen confusing from the earlier posts b/c I cleaned out all the tables name **_olay.
for inputFC in listFCS: targetFC = inputFC +"_olay" Mapper = GetFieldMappings(inputFC, targetFC, dico) # Process: Append arcpy.Append_management(p.join(ws,inputFC), p.join(ws,targetFC),"NO_TEST",Mapper) print 'Appended "{0}" to "{1}" in {2}'.format(inputFC, targetFC, p.basename(ws))
# Import arcpy module import arcpy arcpy.env.overwriteOutput = True from os import path as p def GetFieldMappings(inputFC, targetFC, dico): field_mappings = arcpy.FieldMappings() field_mappings.addTable(inputFC) for input, output in dico.iteritems(): field_map = arcpy.FieldMap() field_map.addInputField(inputFC, input) field = field_map.outputField field.name = output field_map.outputField = field field_mappings.addFieldMap(field_map) del field, field_map return field_mappings #Variables dico = {'Permanent_Identifier': 'Permanent_ID', 'STRM_NAME': 'LLID_STRM_NAME', 'DATE_SRVY': 'SOURCE_DATE', 'SPPCODE': 'SPECRCODE', 'SRC_AGENCY': 'SOURCE_AGENCY', 'SRC_WHO': 'SOURCE_WHO'} arcpy.env.workspace = r"G:\ChrisGIS\COMBO_fishdist_events" for ws in arcpy.ListWorkspaces("*", "FileGDB"): arcpy.env.workspace = ws listFCS = arcpy.ListTables("WDFW*evt") for inputFC in listFCS: targetFC = inputFC +"_olay" Mapper = GetFieldMappings(inputFC, targetFC, dico) # Process: Append arcpy.CreateTable_management(ws, targetFC)) arcpy.Append_management(p.join(ws,inputFC), p.join(ws,targetFC),"NO_TEST",Mapper) print 'Appended "{0}" to "{1}" in {2}'.format(inputFC, targetFC, p.basename(ws))