Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in Python by (1.6k points)

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?

1 Answer

0 votes
by (25.1k points)

You can get user input using the input method in python 3 and raw_input in python 2 e.g.: 

data = input("Enter your name") # for python 3

data = raw_input("Enter your name") # for python 2

For command line arguments you can use the sys.argv like this:

import sys

print(sys.argv)

Note that sys.argv is a list with the first element as the file name and rest of the elements are arguments passed by the user.

Related questions

0 votes
1 answer
asked Sep 23, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...