list APRXs in workspace folder

493
3
01-23-2019 11:44 AM
JaredPilbeam2
MVP Regular Contributor

I'm in the process of migrating my stand-alone python scripts from ArcMap to ArcGIS Pro. Using PyScripter as an IDE, I was able to successfully set up my environment. I'm using Python 3.6.2 that came with Pro 2.1. 

I want to simply list the files in the workspace folder. Line 3 causes an OSError.

import arcpy, os

aprx = arcpy.mp.ArcGISProject(r"Path\to\folder")

#list the APRXs of the workspace folder
for root, dirs, files, in os.walk(aprx):
    for f in files:
        if f.endswith(".aprx"):
            mxd = arcpy.mp.ArcGISProject(os.path.join(root, f))
            print("current map being checked is  " + f)
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

How do I create a variable for the workspace folder for Pro? Here's how I would've done it for ArcMap:

ws = arcpy.env.workspace = r'path\to\folder'
0 Kudos
3 Replies
DarrenWiens2
MVP Honored Contributor

It looks like you should reference a aprx file, not directory. For example:

aprx = arcpy.mp.ArcGISProject(r"C:\Projects\blank.aprx")

Also, os.walk doesn't specifically have anything to do with ArcGIS or arcpy. Pass it a plain old directory string. For example:

rootDir = '.'
for dirName, subdirList, fileList in os.walk(rootDir):
    ...
DanPatterson_Retired
MVP Esteemed Contributor

that returns a project

from arcpy import mp as MAP  
aprx = MAP.ArcGISProject('c:/Projects/YosemiteNP/Yosemite.aprx')

#or

from arcpy import mp
aprx = mp.ArcGISProject('c:/Projects/YosemiteNP/Yosemite.aprx')

# ---- to start in that folder

"/".join(aprx.split("/")[:-1])

'c:/Projects/YosemiteNP'
JaredPilbeam2
MVP Regular Contributor

Thanks for the help.

Apologies if I wasn't too clear. I wanted to list any and all projects (APRXs) in a folder. Sometimes spelling it out answers your own question.

It looks like you can use the same environment variable as you would with ArcPy in ArcMap. This worked:

ws = arcpy.env.workspace = r"path\to\folder"

Whole thing:

import arcpy, os

ws = arcpy.env.workspace = r"path\to\folder"

#list the APRXs of the workspace folder
for root, dirs, files, in os.walk(ws):
    for f in files:
        if f.endswith(".aprx"):
            print("current project being checked is  " + f)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos