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?
This question is related to
python
input
command-line-arguments
This simple program helps you in understanding how to feed the user input from command line and to show help on passing invalid argument.
import argparse
import sys
try:
parser = argparse.ArgumentParser()
parser.add_argument("square", help="display a square of a given number",
type=int)
args = parser.parse_args()
#print the square of user input from cmd line.
print args.square**2
#print all the sys argument passed from cmd line including the program name.
print sys.argv
#print the second argument passed from cmd line; Note it starts from ZERO
print sys.argv[1]
except:
e = sys.exc_info()[0]
print e
1) To find the square root of 5
C:\Users\Desktop>python -i emp.py 5
25
['emp.py', '5']
5
2) Passing invalid argument other than number
C:\Users\bgh37516\Desktop>python -i emp.py five
usage: emp.py [-h] square
emp.py: error: argument square: invalid int value: 'five'
<type 'exceptions.SystemExit'>
In Python 2:
data = raw_input('Enter something: ')
print data
In Python 3:
data = input('Enter something: ')
print(data)
var = raw_input("Please enter something: ")
print "you entered", var
Or for Python 3:
var = input("Please enter something: ")
print("You entered: " + var)
As of Python 3.2 2.7, there is now argparse for processing command line arguments.
import six
if six.PY2:
input = raw_input
print(input("What's your name? "))
If it's a 3.x version then just simply use:
variantname = input()
For example, you want to input 8:
x = input()
8
x will equal 8 but it's going to be a string except if you define it otherwise.
So you can use the convert command, like:
a = int(x) * 1.1343
print(round(a, 2)) # '9.07'
9.07
The best way to process command line arguments is the argparse
module.
Use raw_input()
to get user input. If you import the readline module
your users will have line editing and history.
raw_input
is no longer available in Python 3.x. But raw_input
was renamed input
, so the same functionality exists.
input_var = input("Enter something: ")
print ("you entered " + input_var)
Use 'raw_input' for input from a console/terminal.
if you just want a command line argument like a file name or something e.g.
$ python my_prog.py file_name.txt
then you can use sys.argv...
import sys
print sys.argv
sys.argv is a list where 0 is the program name, so in the above example sys.argv[1] would be "file_name.txt"
If you want to have full on command line options use the optparse module.
Pev
As of Python 3.2 2.7, there is now argparse for processing command line arguments.
This simple program helps you in understanding how to feed the user input from command line and to show help on passing invalid argument.
import argparse
import sys
try:
parser = argparse.ArgumentParser()
parser.add_argument("square", help="display a square of a given number",
type=int)
args = parser.parse_args()
#print the square of user input from cmd line.
print args.square**2
#print all the sys argument passed from cmd line including the program name.
print sys.argv
#print the second argument passed from cmd line; Note it starts from ZERO
print sys.argv[1]
except:
e = sys.exc_info()[0]
print e
1) To find the square root of 5
C:\Users\Desktop>python -i emp.py 5
25
['emp.py', '5']
5
2) Passing invalid argument other than number
C:\Users\bgh37516\Desktop>python -i emp.py five
usage: emp.py [-h] square
emp.py: error: argument square: invalid int value: 'five'
<type 'exceptions.SystemExit'>
Use 'raw_input' for input from a console/terminal.
if you just want a command line argument like a file name or something e.g.
$ python my_prog.py file_name.txt
then you can use sys.argv...
import sys
print sys.argv
sys.argv is a list where 0 is the program name, so in the above example sys.argv[1] would be "file_name.txt"
If you want to have full on command line options use the optparse module.
Pev
The best way to process command line arguments is the argparse
module.
Use raw_input()
to get user input. If you import the readline module
your users will have line editing and history.
If you are running Python <2.7, you need optparse, which as the doc explains will create an interface to the command line arguments that are called when your application is run.
However, in Python =2.7, optparse has been deprecated, and was replaced with the argparse as shown above. A quick example from the docs...
The following code is a Python program that takes a list of integers and produces either the sum or the max:
import argparse
parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('integers', metavar='N', type=int, nargs='+',
help='an integer for the accumulator')
parser.add_argument('--sum', dest='accumulate', action='store_const',
const=sum, default=max,
help='sum the integers (default: find the max)')
args = parser.parse_args()
print args.accumulate(args.integers)
If it's a 3.x version then just simply use:
variantname = input()
For example, you want to input 8:
x = input()
8
x will equal 8 but it's going to be a string except if you define it otherwise.
So you can use the convert command, like:
a = int(x) * 1.1343
print(round(a, 2)) # '9.07'
9.07
Careful not to use the input
function, unless you know what you're doing. Unlike raw_input
, input
will accept any python expression, so it's kinda like eval
var = raw_input("Please enter something: ")
print "you entered", var
Or for Python 3:
var = input("Please enter something: ")
print("You entered: " + var)
The best way to process command line arguments is the argparse
module.
Use raw_input()
to get user input. If you import the readline module
your users will have line editing and history.
Use 'raw_input' for input from a console/terminal.
if you just want a command line argument like a file name or something e.g.
$ python my_prog.py file_name.txt
then you can use sys.argv...
import sys
print sys.argv
sys.argv is a list where 0 is the program name, so in the above example sys.argv[1] would be "file_name.txt"
If you want to have full on command line options use the optparse module.
Pev
In Python 2:
data = raw_input('Enter something: ')
print data
In Python 3:
data = input('Enter something: ')
print(data)
raw_input
is no longer available in Python 3.x. But raw_input
was renamed input
, so the same functionality exists.
input_var = input("Enter something: ")
print ("you entered " + input_var)
Careful not to use the input
function, unless you know what you're doing. Unlike raw_input
, input
will accept any python expression, so it's kinda like eval
import six
if six.PY2:
input = raw_input
print(input("What's your name? "))
var = raw_input("Please enter something: ")
print "you entered", var
Or for Python 3:
var = input("Please enter something: ")
print("You entered: " + var)
The best way to process command line arguments is the argparse
module.
Use raw_input()
to get user input. If you import the readline module
your users will have line editing and history.
Use 'raw_input' for input from a console/terminal.
if you just want a command line argument like a file name or something e.g.
$ python my_prog.py file_name.txt
then you can use sys.argv...
import sys
print sys.argv
sys.argv is a list where 0 is the program name, so in the above example sys.argv[1] would be "file_name.txt"
If you want to have full on command line options use the optparse module.
Pev
var = raw_input("Please enter something: ")
print "you entered", var
Or for Python 3:
var = input("Please enter something: ")
print("You entered: " + var)
If you are running Python <2.7, you need optparse, which as the doc explains will create an interface to the command line arguments that are called when your application is run.
However, in Python =2.7, optparse has been deprecated, and was replaced with the argparse as shown above. A quick example from the docs...
The following code is a Python program that takes a list of integers and produces either the sum or the max:
import argparse
parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('integers', metavar='N', type=int, nargs='+',
help='an integer for the accumulator')
parser.add_argument('--sum', dest='accumulate', action='store_const',
const=sum, default=max,
help='sum the integers (default: find the max)')
args = parser.parse_args()
print args.accumulate(args.integers)
Source: Stackoverflow.com