1

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)
2
  • There are a lot of indentation errors. Please fix them first. Commented Feb 10, 2022 at 13:59
  • 1
    To get rid of the glob error, add import glob to import section. Commented Feb 10, 2022 at 14:07

1 Answer 1

6

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)
4
  • Thanks BERA. I tried that but I get the error message below; Traceback (most recent call last): File "H:\002_WIP\32_WIP_MGrizzly\Ref\Py\py\py\FCListFromAPRXcsvTEST.py", line 24, in <module> aprx = arcpy.mp.ArcGISProject(proj) AttributeError: 'module' object has no attribute 'mp' Commented Feb 10, 2022 at 17:48
  • I dont know why. I tried it and it worked. Maybe you have an old ArcGIS Pro version?
    – Bera
    Commented Feb 10, 2022 at 18:06
  • 1
    It worked now. Many thanks BERA and all. Commented Feb 11, 2022 at 9:52
  • Nice, please accept my answer with the checkbox
    – Bera
    Commented Feb 12, 2022 at 15:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.