Select to view content in your preferred language

Print list of maps and layers across multiple ArcGISProject()'s

659
4
09-27-2023 09:59 AM
Labels (3)
JackVan_Schenck
New Contributor

Hi guys!  My subject is pretty descriptive, but trying to figure out how to get this basic script to run!  I've attempted running it from the Python windows within a Pro session and outside from the command line.

Script:

import arcpy

with open(r"C:\GISData\aprx_list.txt") as aprx_files:
for aprxPathFile in aprx_files:
print(aprxPathFile)
aprx = arcpy.mp.ArcGISProject(aprxPathFile)
for m in aprx.listMaps():
print("Map: " + m.name)
for lyr in m.listLayers():
print(" " + lyr.name)

 

Contents of "aprx_list.txt" is:

C:\\Users\\gis_user\\OneDrive - Company\\Documents\\ArcGIS\\Projects\\fixit\\fixit.aprx
C:\\Users\\gis_user\\OneDrive - Company\\Documents\\ArcGIS\\Projects\\MyProject\\MyProject.aprx
C:\\Users\\gis_user\\OneDrive - Company\\Documents\\ArcGIS\\Projects\\MyProject1\\MyProject1.aprx
C:\\Users\\gis_user\\OneDrive - Company\\Documents\\ArcGIS\\Projects\\MyProject2\\MyProject2.aprx

 

And, the result and error I'm getting from both environments is:

C:\\Users\\gis_user\\OneDrive - Company\\Documents\\ArcGIS\\Projects\\fixit\\fixit.aprx

Traceback (most recent call last):
File "<string>", line 6, in <module>
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\_mp.py", line 536, in __init__
self._arc_object = arcgisscripting._mapping.ArcGISProject(*gp_fixargs((aprx_path,), True))
OSError: C:\\Users\\gis_user\\OneDrive - Company\\Documents\\ArcGIS\\Projects\\fixit\\fixit.aprx

Thanks in advance for the help!

0 Kudos
4 Replies
MobiusSnake
MVP

Do those files definitely exist?  That's the message you'll get when trying to open an APRX that doesn't exist.  You can use arcpy.Exists() to check that a file exists before opening it.

0 Kudos
JackVan_Schenck
New Contributor

Good question - they definitely exist (although I whiteboarded the gis_user and Company parts of the paths in this post for privacy).  Came from doing an old school "dir /s /b > aprx_list.txt", then doing a find and replace to get the double slashes for Python's persnickety behavior with spaces.

0 Kudos
JackVan_Schenck
New Contributor

Hey @MobiusSnake - I just put a file.exists() through os.path... looks like I just need to escape those "\\" delimiters from the text file!

0 Kudos
JackVan_Schenck
New Contributor

Well, @MobiusSnake got me on the right "path" here ;-).  I've tried feeding the path and file into os.path.isfile() in a number of tricky ways... here is my latest attempt:

# For loop with file listing
import arcpy, os.path

with open(r"C:\GISData\aprx_list.txt") as aprx_files:
  for aprxPathFile in aprx_files:
    aprxPathFile = 'r{}'.format(aprxPathFile)
    print(aprxPathFile)
    if os.path.isfile(aprxPathFile):
      aprx = arcpy.mp.ArcGISProject(aprxPathFile)
      for m in aprx.listMaps():
        print("Map: " + m.name)
        for lyr in m.listLayers():
          print(" " + lyr.name)

The print(aprxPathFile) prints out the results as:

r"C:\Users\gis_user\OneDrive - Company\Documents\ArcGIS\Projects\fixit\fixit.aprx"

r"C:\Users\gis_user\OneDrive - Company\Documents\ArcGIS\Projects\MyProject\MyProject.aprx"

r"C:\Users\gis_user\OneDrive - Company\Documents\ArcGIS\Projects\MyProject1\MyProject1.aprx"

r"C:\Users\gis_user\OneDrive - Company\Documents\ArcGIS\Projects\MyProject2\MyProject2.aprx"

But os.path.isfile(aprxPathFile) returns False.  Spooky part?  I can copy the path and file text from one of these entries, paste it in to os.path.isfile() and I get True! 

0 Kudos