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)
Solved! Go to Solution.
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...
Hey! Thanks for saving us some work! Mucho appreciated!
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...
@Luke_Pinner That is a great add! We don't do any custom tool boxes here definitely a great add for those who do.
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.