1

I want to set the default value of one parameter based on another one. The files might be moved between different locations, but their relative locations would be the same. For example, file A is at C:\test\model, and file B at C:\test\tiger. Therefore, I want to use os.path.join(os.path.dirname(fileB), "tiger") to set the default value.

I tried in the Python Toolbox as the following code, but it didn't work and gave me the error message.

import arcpy, os

class Toolbox(object):
    def __init__(self):
        self.label =  "Model"
        self.alias  = ""

        # List of tool classes associated with this toolbox
        self.tools = [step1]

class step1(object):
    def __init__(self):
        self.label       = "step 1"
        self.description = ""

    def getParameterInfo(self):
        #Define parameter definitions
        # Model folder parameter
        model_folder = arcpy.Parameter(
            displayName="Model Location",
            name="model_folder",
            datatype="Folder",
            parameterType="Required",
            direction="Input")
        model_folder.value = r'C:\model'

        # Tiger dataset folder parameter
        tiger_folder = arcpy.Parameter(
            displayName="TIGER",
            name="tiger_folder",
            datatype="Folder",
            parameterType="Required",
            direction="Input",
            enabled=True)

        parameters = [model_folder,
                      tiger_folder
                      ]

        return parameters

    def isLicensed(self): #optional
        return True

    def updateParameters(self, parameters): #optional
        parameters[1].value = os.path.join(os.path.dirname(parameters[0].value), "tiger")


    def updateMessages(self, parameters): #optional
        return

    def execute(self, parameters, messages):
        return

Errors:

Close WindowERROR Traceback (most recent call last): File "", line 219, in updateParameters File "C:\Python27\ArcGIS10.3\Lib\ntpath.py", line 205, in dirname return split(p)[0] File "C:\Python27\ArcGIS10.3\Lib\ntpath.py", line 170, in split d, p = splitdrive(p) File "C:\Python27\ArcGIS10.3\Lib\ntpath.py", line 125, in splitdrive if p[1:2] == ':': TypeError: 'geoprocessing value object' object has no attribute 'getitem'

I also add several self-defined functions to help me manipulate with the parameters interaction, but they didn't work. It seems like in the updateParameters of Python Toolbox, we can't use self-defined functions.

Any suggestions? Using Advanced 10.3.1 ArcGIS.

3

The os.path methods operate on strings. You are passing a Value object not a string to os.path.dirname. Use parameters[0].valueAsText or parameters[0].value.value instead of parameters[0].value.

i.e.

    parameters[1].value = os.path.join(os.path.dirname(parameters[0].valueAsText), "tiger")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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