How to access command-line arguments in Python

In a C program, the command-line arguments passed to the program can be accessed using the argc and argv input parameters to the main function. Accessing the command-line arguments passed to a Python program are easy to access too. These arguments are available as a list named argv in the sys module. At a minimum, there is at least one argument in this list. This is the first argument, which is the name of the Python script.

This Python program prints out its command-line arguments:

Advertisements

One thought on “How to access command-line arguments in Python

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.