Tutorial

Python Command Line Arguments

Published on August 3, 2022
author

Pankaj

Python Command Line Arguments

Python Command line arguments are input parameters passed to the script when executing them. Almost all programming language provide support for command line arguments. Then we also have command line options to set some specific options for the program.

Python Command Line Arguments

There are many options to read python command line arguments. The three most common ones are:

  1. Python sys.argv
  2. Python getopt module
  3. Python argparse module

Let’s look through simple program to learn how to read and use python command line arguments.

Python sys module

Python sys module stores the command line arguments into a list, we can access it using sys.argv. This is very useful and simple way to read command line arguments as String. Let’s look at a simple example to read and print command line arguments using python sys module.

import sys

print(type(sys.argv))
print('The command line arguments are:')
for i in sys.argv:
    print(i)

Below image illustrates the output of a sample run of above program. read python command line argument using sys.argv There are many other useful functions in sys module, read them in detail at python sys module.

Python getopt module

Python getopt module is very similar in working as the C getopt() function for parsing command-line parameters. Python getopt module is useful in parsing command line arguments where we want user to enter some options too. Let’s look at a simple example to understand this.

import getopt
import sys

argv = sys.argv[1:]
try:
    opts, args = getopt.getopt(argv, 'hm:d', ['help', 'my_file='])
    print(opts)
    print(args)
except getopt.GetoptError:
    # Print a message or do something useful
    print('Something went wrong!')
    sys.exit(2)

Above example is very simple, but we can easily extend it to do various things. For example, if help option is passed then print some user friendly message and exit. Here getopt module will automatically parse the option value and map them. Below image shows a sample run. parse command line argument in python using getopt module To learn more, read python getopt module.

Python argparse module

Python argparse module is the preferred way to parse command line arguments. It provides a lot of option such as positional arguments, default value for arguments, help message, specifying data type of argument etc. At the very simplest form, we can use it like below.

import argparse

parser = argparse.ArgumentParser()
parser.parse_args()

Below is the output from quick run of above script. python command line arguments using argparse module Python argparse module provide a lot many features, you should read about them at python argparse tutorial for clear understanding. That’s all for different options to read and parse command line arguments in python, you should decide which is the one for your specific requirements and then use it. References:

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Pankaj

author

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.