Setting input and output parameters for custom python script

7960
8
01-18-2011 08:51 AM
DwightWilhelm
New Contributor
I have a very simple bit of python code that reads in a pipe delimited text file and outputs a csv file. This little script sits at the front end of a modelbuilder model and is a precondition for running a TableToTable process. What I need is for a user who runs the model to be able to set the input file name and path and also set the output file name and path. Any help would be appreciated.

output = open('gnis.csv', "a")
for line in open('GNIS.txt'):
    line=line.replace("|", ",")
    output.write(line)
output.close()
0 Kudos
8 Replies
DaleHoneycutt
Occasional Contributor III
See A Quick Tour of Creating Script Tools (10.0 doc, but applies to 9.3 as well)

For 9.3, see An Overview Of Script Tools
0 Kudos
DwightWilhelm
New Contributor
See A Quick Tour of Creating Script Tools (10.0 doc, but applies to 9.3 as well)

For 9.3, see An Overview Of Script Tools


I've read those articles, but there isn't anything in there that specifically says how to modify existing code to work. The examples are short and appear to be unrelated to what I am doing. Moreover, they don't put all the pieces together to make sense. Everything is in a separate section with no context.
0 Kudos
JeremyNicoletti
New Contributor
I think this link might elaborate more on your question:

http://resources.esri.com/help/9.3/ArcGISDesktop/dotnet/e7d06ae9-a6d1-4248-a7a3-9d5f375f088c.htm

Apparently it's all in the tutorials mentioned above, but those can be difficult to navigate at times.

I'm working on the same problem, and I'll post more as I learn.
0 Kudos
NobbirAhmed
Esri Regular Contributor
@Jeremy: The link you posted is for creating tools in .NET (C# or VB.Net).

@Dwight:
Here is the dummy code for the tool you are envisioning. However, I wonder why you need two parameters to get the input text file! One parameter of type 'Text File' should do. The output parameter (5th parameter of the tool) is set after you convert the lines (SetParameterAsText). You won't see it on tool dialog as its type is set to 'Derived'. However, if you drag-drop the tool on a Model - you'll see the output is automatically added to chain this tool to following tools.

Anyway, if it is not a one-time solution you are looking for, it is recommended that you dig in the links Dale posted.

import arcpy
import os

in_path = arcpy.GetParameterAsText(0)
in_file = arcpy.GetParameterAsText(1)
in_text_file = os.path.join(in_path, in_file)

out_path = arcpy.GetParameterAsText(2)
out_file = arcpy.GetParameterAsText(3)
out_csv_file = os.path.join(out_path, out_file)

try:
    #output = open(out_csv_file, "a")
    #inputfile = open(in_text_file)
    #for line in inputfile:
    #    line = line.replace("|", ",")
    #    output.write(line)
    #
    #inputfile.close()
    #output.close()

    arcpy.SetParameterAsText(4, out_csv_file)

    arcpy.AddMessage("done")
    
except Exception as ex:
    arcpy.AddError(ex)
0 Kudos
JoelCalhoun
New Contributor III
A few years ago I put together a powerpoint that is a very basic intro to modelbuilder and python (a bit outdated now) that also shows how to create a script tool.
Creating a script tool at version 10 is basically the same so hopefully most of the screen shots should still apply.
Once you have your script tool built, you can simply add it to modelbuilder just like you would any other ESRI tool.

*It might be slightly difficult to read, I don't think it converted to a pdf very well.

Joel
0 Kudos
JeremyNicoletti
New Contributor
Ok, I have figured it out now.

To do this you need to do two things:

Firstly the python code you want is:

xvariable = arcpy.GetParameterAsText(0)
yvariable = arcpy.GetParameterAsText(1)
etc.

If you pull up the help file for "get parameters as text", you will get this code snippet:

-----(Code copies a feature class {infeatureclass} to a location {outlocation}; very straight forward)----------------------
import arcpy
from arcpy import env
import os

# Set the input workspace, get the feature class name to copy
#  and the output location.
#
env.workspace = arcpy.GetParameterAsText(0)
inFeatureClass = arcpy.GetParameterAsText(1)
outLocation = arcpy.GetParameterAsText(2)
outFeatureClass = outLocation + os.sep + os.path.basename(inFeatureClass)

# Copy feature class to output location
#
print "running line: arcpy.Copy_management(inFeatureClass, outFeatureClass)"
arcpy.Copy_management(inFeatureClass, outFeatureClass)
----------

However this is not all, because you have to set the parameters for the script.  So far as I can tell the only way that python distinguishes between which text is supposed to go where is from the order of the parameters, designated by those numbers in parenthesis after "getparameterastext".

To set up parameters for a script: after adding it to your custom toolbox right click it and select the parameters tab. Here you can put a name for each parameter in the left column which it will appear in the tool box before running, and after you can select the data type in the right column, which (*I believe) limits the type of input data to match your desired script.  There are many types, so be careful.

I'm still learning, but it appears that the order is what determines which user input field corresponds to, meaning that in the sample script the top parameter in the parameters tab would correspond to environment workspace (env.workspace = arcpy.GetParameterAsText(0)).

I may yet have more to add to this in the near future. It strikes me as strange that this wasn't easier to figure out.
0 Kudos
JeremyNicoletti
New Contributor
Also, be sure to set all output parameters as output in the parameters under the "Direction" field below the parameter list.

The Data Types can be a bother to get straight, but tweaking these may solve errors generated by running your tool with parameters.

I almost wonder if it isn't easier to test any scripts without parameters and then go back to add them in.


So back to the original question:
you could have a user specify a text file (text files are an option in the parameters tab, so I assume that it works)

userinputtextfile = arcpy.GetParameterAsText(0)
output = open('gnis.csv', "a")
for line in userinputtextfile
    line=line.replace("|", ",")
    output.write(line)
output.close()

That should fix your input issue, however the output will be slightly different, something slightly different, like:


userinputtextfile = arcpy.GetParameterAsText(0)
outputcsv = arcpy.GetParameterAsText(1)
output = open(outputcsv, "a")
for line in userinputtextfile
    line=line.replace("|", ",")
    output.write(line)
output.close()

I likely violated some python language parameter, but you get the idea.  Also, I think the suggestions I made might be specific to Arc 10.x, so please take both factors into consideration when editing your code.
0 Kudos
NobbirAhmed
Esri Regular Contributor
Please enclose your code between CODE tags. Click on the '#' button above the text box to get the tags.
0 Kudos