ArcGIS Project Feature Classes – Listing All Feature Classes from a Single ArcGIS Project File into CSV

arcgis-proarcpy

I am trying to obtain a list of feature classes in an ArcGIS Project file. I get the error below;

*** Remote Interpreter Reinitialized  ***
Traceback (most recent call last):
  File "<module1>", line 19, in <module>
NameError: name 'glob' is not defined

I am trying the code below. I know the code below does not include the CSV code part, if someone can help with that too.

import arcpy
import os

aprx_dir = r'H:\007_Services-GViewer\02_GViewer_Updates\2201xx_Engineering\STG_MXDs\New folder\*.aprx'

aprx_dict = {}

for aprx_file in glob.glob(aprx_dir):
    aprx = arcpy.mp.ArcGISProject(aprx_file)
    for m in aprx.listMaps():
        for layer in m.listLayers():
            if layer.supports("DATASOURCE"):
                ds = layer.dataSource       

                if ds not in aprx_dict:
                    aprx_dict[ds] = [aprx_file]
                elif aprx_file in aprx_dict[ds]:
                    pass
                else:
                    aprx_dict[ds].append(aprx_file)

Best Answer

It looks like you are trying to find all map projects and print all layers in each to a csv file?

Try this:

import arcpy, os, csv

project_folder = r'C:\GIS\ArcGIS_Pro_projects'
output_csv_folder = r'C:\GIS\temp'

projects = []
for root, folder, files in os.walk(project_folder): #Find all aprx files in projects_folder
    for file in files:
        if file.endswith('.aprx'):
            projects.append(os.path.join(root, file))

for proj in projects: #For each project file
    aprx = arcpy.mp.ArcGISProject(proj)
    layers = [] #A list to hold layernames
    for m in aprx.listMaps():
        print(m)
        for lyr in m.listLayers(): #For each layer in the map
            layers.append(lyr) #Append name to the list
    with open(os.path.join(output_csv_folder, os.path.basename(proj).replace('.aprx','.csv')), 'w') as myfile: #Write the layerlist to a csv
        wr = csv.writer(myfile, quoting=csv.QUOTE_ALL)
        wr.writerow(layers)