4

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?

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked May 16, 2013 at 8:30
1
  • Add an arcpy.AddMessage("file " + layerfile) after the print layer file, and compare to the results of the print statement Commented May 16, 2013 at 15:32

1 Answer 1

3

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.

answered Dec 18, 2013 at 21:16

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.