I believe the first way with the if else is similar to Duck Typing and the "look before you leap" lbyl non-pythonic methods.
As Curtis said, the latter with the try/except is pythonic:
It's also better to include the specific error type you're expecting so you don't end up passing unexpected errors too. I don't know what type of error this is supposed to produce but here's an example.
try:
Abstract = tree.find("idinfo/descript/abstract").text
except AttributeError:
Abstract = "No abstract"
except:
# Catch for all other unhandled exceptions
raise
[/noparse] block,[/post] but I think you're missing an indent. Hopefully these fixes address both bugs [post=124021]noted above:[/post]if the name of your feature class has the same name as the feature dataset it resides in, it will cause the script to fail. (ex. - Data.gbd>Contours>Contours) Will also terminate if no text is found in the metadata component it is looking for.See changes below in redimport arcpy, sys from xml.etree.ElementTree import ElementTree from arcpy import env env.overwriteOutput = True env.workspace = r"G:\sdeConnections\win_auth\sde_published.sde" AGSHOME = arcpy.GetInstallInfo("Desktop")["InstallDir"] translatorpath = AGSHOME + r"Metadata\Translator\ARCGIS2FGDC.xml" xmlfile = r"C:\Users\mtenold\Downloads\test.xml" # list any standalone feature classes fcList = arcpy.ListFeatureClasses() fcList.sort() for fc in fcList: print fc arcpy.ExportMetadata_conversion(fc, translatorpath, xmlfile) tree = ElementTree() tree.parse(xmlfile) spot = tree.find("idinfo/descript/abstract") if spot: # if spot is True (not None) print spot.text else: print "No abstract" # list feature datasets datasetList = arcpy.ListDatasets("","Feature") datasetList.sort() for dataset in datasetList: print dataset # list feature classes inside the dataset arcpy.env.workspace = dataset fcList = arcpy.ListFeatureClasses() fcList.sort() for fc in fcList: print " " + fc arcpy.ExportMetadata_conversion(fc, translatorpath, xmlfile) tree = ElementTree() tree.parse(xmlfile) spot = tree.find("idinfo/descript/abstract") if spot: # if spot is True (not None) print " " + spot.text else: print " No abstract"
# Create the Geoprocessor object import arcpy, sys, os from xml.etree.ElementTree import ElementTree from arcpy import env # Get input parameters ##env.workspace = arcpy.GetParameterAsText(0) ##xmlfile = arcpy.GetParameterAsText(1) ##outfile = arcpy.GetParameterAsText(2) env.overwriteOutput = True ##AGSHOME = arcpy.GetInstallInfo("Desktop")["InstallDir"] ##translatorpath = AGSHOME + r"Metadata\Translator\ARCGIS2FGDC.xml" #Output file ##outfile=open(outfile,'w') #Dataset list function # list any standalone feature classes env.workspace = r"C:\projects\Vicinity.gdb" AGSHOME = arcpy.GetInstallInfo("Desktop")["InstallDir"] translatorpath = AGSHOME + r"Metadata\Translator\ARCGIS2FGDC.xml" fcList = arcpy.ListFeatureClasses() fcList.sort() for fc in fcList: fcname = str(fc) ## outfile.write(fcname + "\n" + ",") print fc xmlfile = r"C:\projects\Testing\ " + fcname + ".xml" arcpy.UpgradeMetadata_conversion (fc, "ESRIISO_TO_ARCGIS") arcpy.ExportMetadata_conversion(fc, translatorpath, xmlfile) tree = ElementTree() tree.parse(xmlfile) spot = tree.find("abstract") if spot: # if spot is True (not None) print spot.text else: print "No Abstract" spot = tree.find("purpose") if spot: # if spot is True (not None) print spot.text else: print "No Summary" spot = tree.find("datacred") if spot: # if spot is True (not None) print spot.text else: print "No Credits" # list feature datasets datasetList = arcpy.ListDatasets("","Feature") datasetList.sort() for dataset in datasetList: print dataset # list feature classes inside the dataset arcpy.env.workspace = dataset fcList = arcpy.ListFeatureClasses() fcList.sort() for fc in fcList: print " " + fc arcpy.ExportMetadata_conversion(fc, translatorpath, xmlfile) tree = ElementTree() tree.parse(xmlfile) spot = tree.find("descript/abstract") if spot: # if spot is True (not None) print " " + spot.text else: print " No abstract"
relevant resources:
What is the Metadata module—ArcGIS Pro | Documentation
GitHub - ucd-cws/arcpy_metadata: Python metadata editing classes for ArcGIS feature classes
using arcpy.da.walk to inventory data and export metadata to csv
Python Errors in IDLE when processing metadata
Access and update map and layer metadata (arcpy.mp)
python - updating metadata for feature classes programatically using arcpy - Stack Overflow