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

0 votes
How do I have a Python script that a) can accept user input and how do I make it b) read in arguments if run from the command line?
Jan 5, 2021 in Python by Rajiv
• 8,870 points
1,907 views

1 answer to this question.

0 votes

To read user input you can try the cmd module for easily creating a mini-command line interpreter (with help texts and autocompletion) and raw_input (input for Python 3+) for reading a line of text from the user.

text = raw_input("prompt")  # Python 2
text = input("prompt")  # Python 3

Command-line inputs are in sys.argv. Try this in your script:

import sys
print (sys.argv)

There are two modules for parsing command-line options: (deprecated since Python 2.7, use argparse instead) and get opt. If you just want to input files to your script, behold the power of file input

answered Jan 5, 2021 by Gitika
• 65,770 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 Jun 4, 2018 in Python by aryya
• 7,460 points
812 views
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,510 points
555 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,684 views
0 votes
1 answer

How to Pivot pandas for removing of some headers and renaming of some indexes?

Solution is add parameter values to pivot, then add reset_index for column ...READ MORE

answered Sep 27, 2018 in Python by Priyaj
• 58,020 points
21,636 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,434 views
0 votes
1 answer
0 votes
1 answer
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