Script tool Data List paths

783
2
Jump to solution
06-19-2013 11:32 AM
JimHall
New Contributor III
Is there a better way to use the multivalue property in a script tool?  This code works except when the paths to folders have spaces.

# Script arguments CensusDataList = string.split(arcpy.GetParameterAsText(0), ";")  #Loop through each state directory in the list to create an input value. for CensusState in CensusDataList:     CensusFeaturePath = CensusState + "\\bndrygbs.mdb\\hzCounty"     hzCounty_Inputs = hzCounty_Inputs + ";" + CensusFeaturePath 


If the paths for  CensusDataList are something like:
Path 1 
Path 2
I get  'Path 1';'Path 2' for the list.

If the paths are something like:
Path1 
Path2
I get Path1;Path2 for the list.
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
curtvprice
MVP Esteemed Contributor
Is there a better way to use the multivalue property in a script tool?


You could use GetParameter() instead of GetParameterAsText() and process it as a value table. This is the only approach for example, with a list of layer names if you want to handle layers that contain ";".  It's a lot more complicated to work with parameter objects, and harder to debug, so I try to avoid doing it that way.

Are you sure you are getting quotes? The best way to check is with repr:

CensusDataList = arcpy.GetParameterAsText(0).split(";") for d in CensusDataList:     print repr(d)



If you do indeed have extra quotes, you could strip them like this:

CensusDataList = string.split(arcpy.GetParameterAsText(0), ";") CensusDataList = [d.strip("'") for d in CensusDataList] 


And avoid spaces in paths -- just trouble as far as I'm concerned!

One more thing, in arcpy, tools can read lists, so you don't need to join them back together with ";" delimiters:

#Loop through each state directory in the list to create an input value (another list) hzCounties = ["{}\\bndrygbs.mdb\\hzCounty".format(p)                      for p in CensusDataList]

View solution in original post

0 Kudos
2 Replies
curtvprice
MVP Esteemed Contributor
Is there a better way to use the multivalue property in a script tool?


You could use GetParameter() instead of GetParameterAsText() and process it as a value table. This is the only approach for example, with a list of layer names if you want to handle layers that contain ";".  It's a lot more complicated to work with parameter objects, and harder to debug, so I try to avoid doing it that way.

Are you sure you are getting quotes? The best way to check is with repr:

CensusDataList = arcpy.GetParameterAsText(0).split(";") for d in CensusDataList:     print repr(d)



If you do indeed have extra quotes, you could strip them like this:

CensusDataList = string.split(arcpy.GetParameterAsText(0), ";") CensusDataList = [d.strip("'") for d in CensusDataList] 


And avoid spaces in paths -- just trouble as far as I'm concerned!

One more thing, in arcpy, tools can read lists, so you don't need to join them back together with ";" delimiters:

#Loop through each state directory in the list to create an input value (another list) hzCounties = ["{}\\bndrygbs.mdb\\hzCounty".format(p)                      for p in CensusDataList]
0 Kudos
JimHall
New Contributor III
It is the script tool form that is adding in the extra quote only when needed.  This or the spaces break the tool inputs further in my code.  I did the following which allows paths to folders to have spaces or not and generate the list of data for processing. I left in my old code commented out for comparison.


#the script tool is looking for multiple folders here
CensusDataList = string.split(arcpy.GetParameterAsText(0), ";")

CensusDataList = [d.strip("'") for d in CensusDataList]

#Loop through each state directory in the list to create input values.

#for CensusState in CensusDataList:
    #CensusFeaturePath = CensusState + "\\bndrygbs.mdb\\hzCounty"
    #hzCounty_Inputs = hzCounty_Inputs + ";" + CensusFeaturePath
hzCounty_Inputs = ["{0}\\bndrygbs.mdb\\hzCounty".format(p)
                    for p in CensusDataList]



I agree that spaces should not be used in paths but.....  :rolleyes:

Thanks for your help!
0 Kudos