Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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?

share|improve this question
    
Add an arcpy.AddMessage("file " + layerfile) after the print layer file, and compare to the results of the print statement –  Dan Patterson May 16 '13 at 15:32
add comment

1 Answer

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.