0

I am trying to specify a file location for an output file when creating a python toolbox tool, but the tool continues to output the file to the default geodatabase. I have tried to set the env.workspace within the tool, but clearly this is not the right approach. Here is a code snippet for the parameters I have created to specify the outfolder and the outfile name:

def getParameterInfo(self):
    """Define parameter definitions"""
    # First parameter. Input file
    inFile = arcpy.Parameter(
        displayName="Input File",
        name="inFile",
        datatype="GPFeatureLayer",
        parameterType="Required",
        direction="Input")
    inFile.filter.list = ["Point"] ## restrict to point files
    
    # Second parameter. outfolder
    outFolder = arcpy.Parameter(
        displayName="Output Folder",
        name="outFolder",
        datatype="DEWorkspace",
        parameterType="Required",
        direction="Input")
        
    # Input File Parameter
    outFile = arcpy.Parameter(
        displayName="Output File",
        name="outFile",
        datatype="DEFile",
        parameterType="Required",
        direction="Output")

Unfortunately, the tool continues to write to the default database.

How do I change this behavior?

1
  • Why are you doing this in validation script, when this can be easily done in source code?
    – FelixIP
    Jul 11, 2020 at 1:35

1 Answer 1

1

A DEFile is a file on a disk, not a feature class or table inside a geodatabase.

You can either set the outFile to a string and then join outFolder and outFile.

def getParameterInfo(self):
    """Define parameter definitions"""
    inFile = arcpy.Parameter(
        displayName="Input File",
        name="inFile",
        datatype="GPFeatureLayer",
        parameterType="Required",
        direction="Input")
    inFile.filter.list = ["Point"]

    outFolder = arcpy.Parameter(
        displayName="Output Folder",
        name="outFolder",
        datatype="DEWorkspace",
        parameterType="Required",
        direction="Input")
    outFolder.type = ["Local Database"]
        
    outFile = arcpy.Parameter(
        displayName="Output File",
        name="outFile",
        datatype="GPString",
        parameterType="Required",
        direction="Input")

def execute(self, parameters, messages):
    """Creates a line from the in_feature to the closest near feature,
    within the search tolerance."""
    in_features = parameters[0].valueAsText
    out_folder = parameters[1].valueAsText
    out_file = parameters[2].valueAsText

    arcpy.env.overwriteOutput = True

    desc = arcpy.Describe(in_features)
    # Create output feature class
    arcpy.management.CreateFeatureclass(
        out_folder, out_file, geometry_type="POINT",
        spatial_reference=desc.spatialReference)   

If your geodatabase already exists, then you could just prompt for the output (DEFeatureClass or DETable). Then use the path from that object.

def getParameterInfo(self):
    """Define parameter definitions"""
    inFile = arcpy.Parameter(
        displayName="Input File",
        name="inFile",
        datatype="GPFeatureLayer",
        parameterType="Required",
        direction="Input")
    inFile.filter.list = ["Point"]

    out_feature_class = arcpy.Parameter(
        name='out_feature_class',
        displayName='Output Feature Class',
        parameterType='Required',
        direction='Output',
        datatype='DEFeatureClass',
        multiValue=False)

def execute(self, parameters, messages):
    """Creates a line from the in_feature to the closest near feature,
    within the search tolerance."""
    in_features = parameters[0].valueAsText
    out_feature_class = parameters[1].valueAsText

    arcpy.env.overwriteOutput = True

    desc = arcpy.Describe(in_features)
    path, name = os.path.split(out_feature_class)
    # Create output feature class
    arcpy.management.CreateFeatureclass(
        path, name, geometry_type="POINT",
        spatial_reference=desc.spatialReference)  

Your Answer

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

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