7.1. Command-Line Arguments

We already encountered the sys module when we printed out the path variable as part of the Python existence proof in chapter 2. The sys module provides the variable argv for accessing command-line arguments. The argv variable is an example of a Python list. A list is simply an ordered collection of objects. Lists are delimited by square brackets, and items in a list are separated by a “,”. Here is a list consisting of integers 1, 2, and 3.

CD-ROM reference=7001.txt
[1,2,3]

Here is a list consisting of the string "Hello", the floating point number 3.14, and the integer 1.

CD-ROM reference=7002.txt
["Hello",3.14,1]

The argv variable is a list of strings–one for each command-line argument. When Python is executed ...

Get XML Processing with Python 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.