Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 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
Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94.1k users

Browse Categories

...