Run ArcPy tool dynamically in python

Document created by s.offermannconterra-de-esridist Employee on Oct 6, 2014
Version 1Show Document
  • View in full screen mode

Did you ever wanted to run a tool in Python dynamically? Without "hard coding" the tool name and the parameters in a script?

 

This is the normal, static syntax to call a tool:

result = arcpy.Buffer_management("input", "output", 100)

 

If you need to run a tool depending on a condition, and you want to make the name and the parameters dynamic, you can use this syntax:

tool = "Buffer_management"
arguments = ["input", "output", 100]
result = getattr(arcpy, tool)(*arguments)

 

So the built-in gettattr-method calls the method tool with arguments as parameter values, the asterisk is used to unpack the list of arguments for the call of the tool.

1 person found this helpful

Attachments

    Outcomes