简体   繁体   中英

runscript with integer input in python

I want to run from a terminal a python code that required an integer input in my code such as

python code.py 4

I saw that the input() function works when I do

python code.py

4

but I have a script that run many times the same code with different integers so I would like to use something where I could run my code at the same time as I feed the integer.

Any idea how ?

Many thanks, Ele

you can use the sys import to read from the sys.argv list to get commandline arguments.

from tutorialspoint documentation on cmd arguments

import sys

print 'Number of arguments:', len(sys.argv), 'arguments.'
print 'Argument List:', str(sys.argv)

in your case :

import sys

value = sys.argv[1] #argv[0] is the name of the script

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM