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 in Python by Rajiv
• 8,880 points
154 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 by Gitika
• 65,870 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,440 points
166 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,550 points
107 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
2,949 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,120 points
10,311 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 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 2,032 views
0 votes
0 answers
0 votes
1 answer