Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

How to access command-line arguments in Python

📅 2014-Feb-25 ⬩ ✍️ Ashwin Nanjappa ⬩ 📚 Archive

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:

import sys

def main():
    for arg in sys.argv:
        print arg

if "__main__" == __name__:
    main()