AnsweredAssumed Answered

using arcpy.da.walk to inventory data and export metadata to csv

Question asked by sgroff on Nov 30, 2016
Latest reply on Aug 21, 2020 by blake.terhune

I'm a novice when it comes to arcpy and am trying to develop a script that will use arcpy.da.walk to inventory our GIS data. As it goes through the folders/gdbs of data that we have, I want it to export a few items to a csv for each feature class (for now I'd be happy with feature class path, filename, spatial reference name and metadata purpose). I've gotten the script to work up until the metadata purpose part. Once I add the lines:

arcpy.ExportMetadata_conversion(feature_class, translatorpath, xmlfile)   
tree = ElementTree()  
spot = tree.find("idinfo/descript/purpose")

my script does not return anything. Without those lines, I recieve a csv file with feature class path, filename, and spatial reference name, but if I include the lines my csv file is empty. No errors, just empty. My script (included below) is based off of: and

Any help is greatly appreciated!

EDITED: Some feature classes may not have a spatial reference defined, and many feature classes may not have any metadata associated. I still want these in the csv, but those fields can either be blank or say something along the lines of "No spatial reference defined" and "No metadata purpose defined".

import os
import arcpy
import csv
from xml.etree.ElementTree import ElementTree
from arcpy import env

def inventory_data(workspace, datatypes):
    Generates full path names under a catalog tree for all requested

    workspace: string
        The top-level workspace that will be used.
    datatypes: string | list | tuple
        Keyword(s) representing the desired datatypes. A single
        datatype can be expressed as a string, otherwise use
        a list or tuple. See arcpy.da.Walk documentation
        for a full list.

    for path, path_names, data_names in arcpy.da.Walk(
            workspace, datatype=datatypes):
        for data_name in data_names:
            yield os.path.join(path, data_name)

AGSHOME = arcpy.GetInstallInfo("Desktop")["InstallDir"] 
translatorpath = AGSHOME + "Metadata\\Translator\\ARCGIS2FGDC.xml"
outfile = "C:\\GIS\\Records\\Data Management\\Inventories\\GIS_Data_Inventory_daWalk_function_outputtocsv_descitems_try_sr_meta.csv"
xmlfile = "C:\\GIS\\Records\\Data Management\\Inventories\\TempInventoryError\\daWalk_function_outputtocsv_descitems_try_sr_meta.xml"

with open (outfile, 'wb') as csvfile:
    csvwriter = csv.writer(csvfile)
    for feature_class in inventory_data(r"C:\GIS\Data\Natural_Environment\Species_and_Habitats\Habitat_Models", "FeatureClass"):
            desc = arcpy.Describe(feature_class)
            sr = desc.spatialReference
            arcpy.ExportMetadata_conversion(feature_class, translatorpath, xmlfile) 
            tree = ElementTree() 
            spot = tree.find("idinfo/descript/purpose")
            csvwriter.writerow([desc.path.encode('utf-8'), desc.file.encode('utf-8'), desc.dataType.encode('utf-8'),'utf-8'), spot.text.encode('utf-8')])