Adding dynamic parameters to a script
The scripts we have generated in previous chapters have all had hard-coded inputs. The input values were written in the script as strings or numbers and assigned to variables. While they can be updated manually to replace the input and output file paths and SQL statements, programmers should aim to create scripts that will not require editing each time they are used. Instead, scripts should be designed to be dynamic and accept file paths and other inputs as parameters or arguments, in much the same manner that the functions we have created accept parameters.
Python was designed with this in mind, and the sys
module has a method called sys.argv
that accepts inputs passed to the script when it is executed. While the designers of ArcPy and its predecessor arcgisscripting module
initially took advantage of the sys.argv
method, in time they designed an ArcPy method for accepting script parameters. As either method can be used when writing ArcPy scripts, and...