I've created a simple script tool which should list all field names from all feature classes in a geodatabase. I struggle with the correct syntax when including inline variable in the script, which is why the script tool is not doing what it should within the model. Any links with information or suggestions would be kindly appreciated. I've looked in plenty of forums but not been able to make the script work within the modelbuilder. Also tried implementing it in the calculate value tool but it doesn't seem to work.
The script performs fine when not part of the model and only when a shapefile is specified.
Solved! Go to Solution.
Inline variables are meant to replace parameter input in the tool's GUI, not in its code.
To get a parameter value inside the tool's code, use the GetParameter functions.
print() won't show anything inside a tool, as the tool doesn't use the Python Window as output. Use AddMessage instead to output into the tool messages.
import arcpy
if __name__ == "__main__":
in_table = arcpy.GetParameter(0)
for f in arcpy.ListFields(in_table):
arcpy.AddMessage([f.name, f.type])
And then you can easily add it to a model:
If you want something more permanent than a simple print output, you can use this script as a script tool. It will iterate through each table and feature class in a workspace and store the field information in a new table.
# parameter 1: Workspace, input
# parameter 2: Table, output
import arcpy
from pathlib import Path
in_ws = arcpy.GetParameterAsText(0)
out_table = arcpy.GetParameterAsText(1)
# create output table
folder = str(Path(out_table).parent)
name = Path(out_table).stem
out_table = arcpy.management.CreateTable(folder, name)
arcpy.management.AddField(out_table, "TableName", "TEXT")
arcpy.management.AddField(out_table, "FieldName", "TEXT")
arcpy.management.AddField(out_table, "FieldType", "TEXT")
# start writing
with arcpy.da.InsertCursor(out_table, ["TableName", "FieldName", "FieldType"]) as cursor:
# iterate through the workspace's feature classes and tables
walk = arcpy.da.Walk(in_ws, datatype=["FeatureClass","Table"])
for dir_path, dir_names, file_names in walk:
for file_name in file_names:
in_table = str(Path(dir_path) / file_name)
# get the fields
fields = arcpy.ListFields(in_table)
# write the fields into the output table
for f in fields:
row = [in_table, f.name, f.type]
cursor.insertRow(row)
# set the output parameter
arcpy.SetParameter(1, out_table)
Inline variables are meant to replace parameter input in the tool's GUI, not in its code.
To get a parameter value inside the tool's code, use the GetParameter functions.
print() won't show anything inside a tool, as the tool doesn't use the Python Window as output. Use AddMessage instead to output into the tool messages.
import arcpy
if __name__ == "__main__":
in_table = arcpy.GetParameter(0)
for f in arcpy.ListFields(in_table):
arcpy.AddMessage([f.name, f.type])
And then you can easily add it to a model:
If you want something more permanent than a simple print output, you can use this script as a script tool. It will iterate through each table and feature class in a workspace and store the field information in a new table.
# parameter 1: Workspace, input
# parameter 2: Table, output
import arcpy
from pathlib import Path
in_ws = arcpy.GetParameterAsText(0)
out_table = arcpy.GetParameterAsText(1)
# create output table
folder = str(Path(out_table).parent)
name = Path(out_table).stem
out_table = arcpy.management.CreateTable(folder, name)
arcpy.management.AddField(out_table, "TableName", "TEXT")
arcpy.management.AddField(out_table, "FieldName", "TEXT")
arcpy.management.AddField(out_table, "FieldType", "TEXT")
# start writing
with arcpy.da.InsertCursor(out_table, ["TableName", "FieldName", "FieldType"]) as cursor:
# iterate through the workspace's feature classes and tables
walk = arcpy.da.Walk(in_ws, datatype=["FeatureClass","Table"])
for dir_path, dir_names, file_names in walk:
for file_name in file_names:
in_table = str(Path(dir_path) / file_name)
# get the fields
fields = arcpy.ListFields(in_table)
# write the fields into the output table
for f in fields:
row = [in_table, f.name, f.type]
cursor.insertRow(row)
# set the output parameter
arcpy.SetParameter(1, out_table)
Hi Johannes! Wow, you've gone above and beyond to answer this. I'll try it and let you know how I got on. Hopefully this will be useful to other users who come across similar issue 🙂
Yep, all worked fine. Great outputs. The table one will be incredibly useful further than my model. Many thanks for helping me