Using sys.argv[ ] to capture command-line input
Instead of hardcoding your scripts with paths to specific datasets, you can make your scripts more flexible by allowing them to accept input in the form of parameters from the command prompt. These input parameters can be captured using Python's sys.argv[]
object.
Getting ready
Python's sys.argv[]
object allows you to capture input parameters from the command line when a script is executed. We will use an example to illustrate how this works. Take a look at the following screenshot:
Each word must be separated by a space. These words are stored in a zero-based list object called sys.argv[]
. In the sys.argv[]
object, the first item in the list referenced by the 0
index, stores the name of the script. In this case, it would be ListFields.py
. Each successive word is referenced by the next integer. Therefore, the first parameter (c:\ArcpyBook\data
) will be stored in sys.argv[1]
, and the second parameter (Burglaries.shp
) will be stored in...