How can I get my Script Tool to accept multivalue parameters

818
2
01-02-2020 05:58 PM
SheilaNguyen1
New Contributor

I am having all sorts of issues trying to get my stand alone script to be a script tool. 

I am walking through all the folders, sub folder, gdb, dataset and feature classes.  I am having problems with setting my parameter for feature class input in which it doesn't recognize operand and strings if I do the GetParameter() instead of GetParameterAsText(). GetParameterAsText will not let me iterate the multiple feature classes. This is what I have below.

import os, sys, arcpy

#Local Variable
ws = arcpy.env.workspace = arcpy.GetParameterAsText(0)
walk = arcpy.os.walk(ws)
arcpy.env.overwriteOutput = True

# User Input Variable
fcs = arcpy.GetParameter(1)
for thing in fcs:
    arcpy.AddMessage(thing)
company = arcpy.GetParameterAsText(2)
outFolder = arcpy.GetParameterAsText(3)
SQL = arcpy.GetParameterAsText(4)



#Walks through the top folder down to each sub-folder listing all feature class
for dirpath, dirnames, filenames in walk:
    for dirname in dirnames:
        arcpy.env.workspace = os.path.join(dirpath, dirname) #Changes workspace enviroment to see each directory path/ directory name
        #print arcpy.env.workspace 
        for ds in arcpy.ListDatasets('', feature_type="feature"):
            for fc in arcpy.ListFeatureClasses('', '', ds):
                for x in fcs:
                    outshape = x + "_" + company + "_" + time.strftime("%m%d%y") + "_" + ".shp"
                    outpath = os.path.join(outFolder, outshape)
                    if x in fc:
                        arcpy.Select_analysis(x, outpath, SQL)
0 Kudos
2 Replies
DanPatterson_Retired
MVP Emeritus

You have to know how your multivalue parameter is set up.

It is described here.

Working with multivalue inputs—ArcPy Get Started | ArcGIS Desktop 

Toss in print statements to see what the values of your parameters are.  Don't be surprised it they aren't python list, but semicolon delimited text strings 

a = 'a;b;c;d'

a.split(";")

['a', 'b', 'c', 'd']
JoshuaWatson1
New Contributor II

Hi Sheila,

Dan's response above will sort you out with regards to listing the input string values.

Your script could also be improved by using: 

walk = arcpy.da.walk(ws)

instead of...

walk = arcpy.os.walk(ws)

arcpy.da.walk() is typically recommended as "os.walk is file based and does not recognize database contents such as geodatabase feature classes, tables, or rasters". This can simplify your script as you can include the datatype parameter within the 'walk' function call.

Walk—Help | ArcGIS Desktop 

Hope this helps