I have 6 arcpy.GetParameterAsText

The first one is a feature class and the remaining 5 are fields (some being optional).

I plan to use an if statement to check if any fields have been selected for the optional parameters.

I'm new to if statements in arcpy and have had a google. To test if things work I have:

if FWHier == '': print("Its blank")

There are no complaints about the syntax here but it does not return (print) anything.

I'm also wondering how to 'end' and if statement. Equivalent to the END IF in Mapbasic.


To check if a parameter has a value using GetParameterAsText, you can check for blank, as you are, or check the length:

if len(parameters[0].valueAsText) == 0:

In Python, there is no ENDIF. Just pay close attention to the indentation of the if block:

if len(parameters[0].valueAsText) == 0:
    print("It's blank")
    print("It's not blank")
print("Outside of the if block")

When running Python geoprocessing tools, you should use the messages mechanism to display text in the window and to the results log.

import arcpy

fc = arcpy.GetParameterAsText(0)

# Get the count from GetCount's Result object
feature_count = int(arcpy.GetCount_management(fc).getOutput(0))

if feature_count == 0:
    arcpy.AddError("{0} has no features.".format(fc))
    arcpy.AddMessage("{0} has {1} features.".format(fc, feature_count))

Edit: reading the answer @GISge gave, using the simple ifis more Pythonic so I highly recommend using that style instead of checking the length.


Just to add on @Richard Morgan's excellent answer, you can also check whether a parameter has been filled in with this simple syntax:

import arcpy

param = arcpy.GetParameterAsText(0)

if param:
    arcpy.AddMessage("Not blank")


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.