Handy script to analyze all python scripts in a directory for python 3.x

823
4
Jump to solution
03-10-2022 02:56 PM
Greg_Mattis
Occasional Contributor II

Today I began the process to upgrade our python scripts from 2.7 to 3.x and I wanted to utilize the Analyze Tools for Pro  but I didn't want to have to search for every python script in my file share to then manually run it in the tool. Therefore I built this script to walk through a directory, find all the python scripts, and then iterate them through the tool automatically. I felt if I wanted to use this script someone else might want it as well! Hope this helps!

import arcpy, os

searchDir = "\\\\{server}\\{share}\\{folder}"
outputPath = "C:\\Users\\{user name}\\Desktop\\Tool Analysis\\"

Scripts = {}
for subdir, dirs, files in os.walk(searchDir):
    for a in files:
        if a.endswith(".py"):
            FileName = os.path.splitext(a)[0]
            path = os.path.join(subdir,a)
            Scripts[FileName]= path

for b in Scripts:
    name = b
    filepath = Scripts[b]
    outfile = outputPath+name+".txt"
    arcpy.AnalyzeToolsForPro_management(filepath, outfile)

 

Greg Mattis, GISP
GIS Analyst
City of Visalia
Tags (3)
1 Solution

Accepted Solutions
Luke_Pinner
MVP Regular Contributor

And if you want to check Python and custom toolboxes as well, you could:

for subdir, dirs, files in os.walk(searchDir):
    for a in files:
        if os.path.splitext(a)[-1] in (".py", ".pyt", ".tbx"):
            etc...

 

View solution in original post

4 Replies
GeoJason
New Contributor III

Hey! Thanks for saving us some work! Mucho appreciated!

0 Kudos
Luke_Pinner
MVP Regular Contributor

And if you want to check Python and custom toolboxes as well, you could:

for subdir, dirs, files in os.walk(searchDir):
    for a in files:
        if os.path.splitext(a)[-1] in (".py", ".pyt", ".tbx"):
            etc...

 

Greg_Mattis
Occasional Contributor II

@Luke_Pinner That is a great add! We don't do any custom tool boxes here definitely a great add for those who do.

Greg Mattis, GISP
GIS Analyst
City of Visalia
0 Kudos
by Anonymous User
Not applicable

I just tested this - it looks like your code writes adjacent to the output directory? Maybe something like

import arcpy
import os

searchDir = r'../some/path'
outputPath = r'../some/path'

Scripts = {}
for subdir, dirs, files in os.walk(searchDir):
    for a in files:
        if a.endswith(".py"):
            FileName = os.path.splitext(a)[0]
            path = os.path.join(subdir, a)
            Scripts[FileName] = path

for b in Scripts:
    name = b
    filepath = Scripts[b]
    outfile = os.path.join(outputPath, name + ".txt")
    arcpy.AnalyzeToolsForPro_management(filepath, outfile)


It also seemed like a fun code golf. Here's an equivalent that adds simple increment to file name to avoid duplicate collisions (e.g. multiple __init__.py)

 

from os.path import join, split, splitext
from pathlib import Path

from arcpy.management import AnalyzeToolsForPro

in_path = r'.../some/path'
out_path = r'.../some/path'

for i, path in enumerate(filter(lambda p: p.suffix in {".py", ".pyt", ".tbx"}, Path(in_path).rglob("*"))):
    AnalyzeToolsForPro(path.__str__(), join(out_path, splitext(split(path)[1])[0] + f'{i}.txt'))

 

Stackoverflow link discussing the common walk/glob path task.

0 Kudos