1

For the model results in Model Builder, I am writing a python script tool which would write the results into a simple text (.txt) file. The problem I am encountering is that I do not know how to call the in-line model variable substitutions in a python script code. I have a few in-line model variables that I need to use for the results writing, for example, one comes from the Parse Path tool as a "Name_DBF". In code it is defined as route_name. How to complete the code correctly as I am getting syntax errors?

import arcpy
import os
import sys

workspace = r'C:\Users\Aura\Desktop\Andriui'

input_table = arcpy.GetParameterAsText(0)

route_name = %Name_DBF%

dvir_tipas = arcpy.GetParameterAsText(1)

# this is how you create a file in Python
file = open('C:\Users\Aura\Desktop\Andriui\RouteDesc.txt','w')
# create a row object to navigate your table
rows = ap.SearchCursor(input_table) 
row = rows.next()
# for each row in your table, retrieve the value in the 'XXX' field, convert it 
# to a string, write it to your output file, then move to the next line in both 
# your input table and output file for row in rows:
file.write("Maršruto pavadinimas:" + str(route_name) + '\n')
file.write("Maršruto atstumas:" + " " + str(row.Atstumas_M) + " " + "km" + '\n')
file.write("Maršruto reljefo sąskaida:" + " " + str(row.Reljefo_S) + " " + "%" + '\n')
file.write("Maršruto sudėtingumas balais nuo 1 iki 3, kai 1 - lengvas, 3 - sunkus maršrutas" + " " + "-" + " " + str(row.Suapvalint) + '\n')
file.write("Apskaičiuota dviratininko tipui: " + " " + str(dvir_tipas) + " " )
del row, rows
file.close()

print "table field data successfully written into file"

Script tool at the end of model

1
  • I suspect that you can use a Calculate Value tool to take an inline variable and return a string to Python.
    – PolyGeo
    Commented May 10, 2018 at 8:25

1 Answer 1

1

All you need to do in your script is to enclose the inline substitution in quotes.

I created this simple model:

Model

The script code is simply this:

import arcpy
inlyr = "%Name_DBF%"
out = r"c:\scratch\test.shp"
arcpy.CopyFeatures_management(inlyr,out)
arcpy.SetParameterAsText(0,out)
2
  • 1
    Hi, thanks for the tip. I tried to enclose it the same as you did, but when I open my text file, it only says "%Name_DBF%" in that place, but not a real name of the database table... this is what I was wondering whether in scripting we need to use something more differently than in simple calculate field... but i need to use the script tool as it is easier for me to include more lines of text. Commented May 10, 2018 at 13:16
  • Interesting, the above works because it is used in a tool (CopyFeatures). If used in some standard python code, like write to text file as you have done it writes %Name_DBF%. The only way I go the name to pass into the script was to expose a new parameter to your script tool that took a string and got a handle on that using aText = arcpy.GetParameterAsText(0). Then writing to the text file worked as expected.
    – Hornbydd
    Commented May 10, 2018 at 14:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.