I have a working python script:
import arcpy
sdeconnection = arcpy.GetParameterAsText(0)
layerfiles = arcpy.GetParameterAsText(1).split(";")
for layerfile in layerfiles:
print layerfile
lyr = arcpy.mapping.Layer(layerfile)
lyr.replaceDataSource(sdeconnection, "SDE_WORKSPACE", lyr.datasetName, False)
lyr.save()
del lyr
I can run it from the command line with multiple layer files as input (separated by ";").
However I cannot make it work when I the script to add to a toolbox. The script runs as expected if I set "multivalue" to false for the layerfiles parameter.
parameters settings
However when I set "multivalue" to true I get the error:
Start Time: Thu May 16 10:13:47 2013
Running script changeLyrWorkspace...
<type 'exceptions.ValueError'>: Object: CreateObject Layer invalid data source
Failed to execute (changeLyrWorkspace).
Failed at Thu May 16 10:13:47 2013 (Elapsed Time: 0,00 seconds)
I get this error regardless of whether I add one or more layers when I run the script through the toolbox.
EDIT
It turns out that the script works through the GUI if there are no spaces in file name of the layer file.
Are there any workarounds for this problem?
-
Add an arcpy.AddMessage("file " + layerfile) after the print layer file, and compare to the results of the print statementuser681– user6812013年05月16日 15:32:10 +00:00Commented May 16, 2013 at 15:32
1 Answer 1
The multivalue parameter mechanism adds single quotes around parameter values that contain spaces. (This is somewhat vaguely described in the arcpy ValueTable
documentation.) So for instance a parameter with spaces will be:
'two words';oneword
instead of (as you might expect):
two words;oneword
You can strip off any enclosing single quotes with the strip
method:
values = [v.strip("'") for v in parameters[0].valueAsText.split(';')]
You could also access the underlying ValueTable in the parameter directly.
Explore related questions
See similar questions with these tags.