How to read process command line arguments

0 votes
I am originally a C programmer. I have seen numerous tricks and "hacks" to read many different arguments.

What are some of the ways Python programmers can do this?
Jun 4, 2018 in Python by charlie_brown
• 7,720 points
593 views

1 answer to this question.

0 votes

The canonical solution in the standard library is argparse (docs):

Here is an example:

from argparse import ArgumentParser

parser = ArgumentParser()
parser.add_argument("-f", "--file", dest="filename",
                    help="write report to FILE", metavar="FILE")
parser.add_argument("-q", "--quiet",
                    action="store_false", dest="verbose", default=True,
                    help="don't print status messages to stdout")

args = parser.parse_args()

argparse supports (among other things):

  • Multiple options in any order.
  • Short and long options.
  • Default values.
  • Generation of a usage help message
answered Jun 4, 2018 by aryya
• 7,450 points

Related Questions In Python

0 votes
1 answer

How to read/process command line arguments?

The canonical solution in the standard library ...READ MORE

answered Aug 8, 2018 in Python by bug_seeker
• 15,520 points
363 views
0 votes
1 answer

How to prompt for user input and read command-line arguments?

To read user input you can try the cmd module for ...READ MORE

answered Jan 5, 2021 in Python by Gitika
• 65,910 points
1,701 views
0 votes
1 answer

How to input optional arguments in python command line?

Please use this code. if len(sys.argv) == 2: first_log ...READ MORE

answered Jul 11, 2019 in Python by Suri
7,165 views
0 votes
1 answer

How to read a large file, line by line, in Python?

The correct, fully Pythonic way to read ...READ MORE

answered Jul 1, 2019 in Python by SDeb
• 13,300 points
592 views
0 votes
2 answers

How do I access command line arguments in Python?

import sys print(sys.argv) More specifically, if you run python example.py ...READ MORE

answered Jun 25, 2020 in Python by Karan
894 views
+3 votes
5 answers

How to read multiple data files in python

Firstly we will import pandas to read ...READ MORE

answered Apr 6, 2018 in Python by DeepCoder786
• 1,720 points
14,795 views
0 votes
1 answer

How to add a new line in Python?

You can use '\n' for a next ...READ MORE

answered May 2, 2018 in Python by aayushi
• 750 points
1,018 views
0 votes
2 answers

In Python, how do I read a file line-by-line into a list?

readline function help to  read line in ...READ MORE

answered Jun 21, 2020 in Python by sahil
• 580 points
1,476 views
0 votes
1 answer

How to get the size of a string in Python?

If you are talking about the length ...READ MORE

answered Jun 4, 2018 in Python by aryya
• 7,450 points
1,087 views
0 votes
1 answer

How to sort a list of strings?

Basic answer: mylist = ["b", "C", "A"] mylist.sort() This modifies ...READ MORE

answered Jun 4, 2018 in Python by aryya
• 7,450 points
1,254 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP