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:

Getting ready

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[] ...

Get Programming ArcGIS with Python Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.