I am wondering how would one get variables inputted in a python script while opening from cmd prompt? I know using c one would do something like:
我想知道如何在从cmd提示符打开时从python脚本中输入变量?我知道使用c会做类似的事情:
int main( int argc, char **argv ) {
int input1 = argv[ 0 ]
int input2 = argv[ 1 ]
.....
}
how can I achieve the same kind of result in python?
如何在python中实现同样的结果?
6 个解决方案
#1
7
import sys
def main():
input1 = sys.argv[1]
input2 = sys.argv[2]
...
if __name__ == "__main__":
main()
#2
5
Command Line Arguments In Python for serious command line argument handling.
命令行参数在Python中用于严格的命令行参数处理。
use sys.argv for simple access:
使用sys.argv进行简单访问:
http://www.faqs.org/docs/diveintopython/kgp_commandline.html
http://www.faqs.org/docs/diveintopython/kgp_commandline.html
#3
2
The arguments are in sys.argv, the first one sys.argv[0]
is the script name.
参数在sys.argv中,第一个sys.argv [0]是脚本名称。
For more complicated argument parsing you should use argparse (for python >= 2.7). Previous modules for that purpose were getopts and optparse.
对于更复杂的参数解析,您应该使用argparse(对于python> = 2.7)。以前用于此目的的模块是getopts和optparse。
#4
1
Check these links
检查这些链接
http://www.faqs.org/docs/diveintopython/kgp_commandline.html
http://www.faqs.org/docs/diveintopython/kgp_commandline.html
Command Line Arguments In Python
Python中的命令行参数
#6
0
it is also useful to determine option specific variables
确定选项特定变量也很有用
''' \
USAGE: python script.py -i1 input1 -i2 input2
-i1 input1 : input1 variable
-i2 input2 : input2 variable
'''
import sys
...
in_arr = sys.argv
if '-i1' not in in_arr or '-i2' not in in_arr:
print (__doc__)
raise NameError('error: input options are not provided')
else:
inpu1 = in_arr[in_arr.index('-i1') + 1]
inpu2 = in_arr[in_arr.index('-i2') + 1]
...
# python script.py -i1 Input1 -i2 Input2
#1
7
import sys
def main():
input1 = sys.argv[1]
input2 = sys.argv[2]
...
if __name__ == "__main__":
main()
#2
5
Command Line Arguments In Python for serious command line argument handling.
命令行参数在Python中用于严格的命令行参数处理。
use sys.argv for simple access:
使用sys.argv进行简单访问:
http://www.faqs.org/docs/diveintopython/kgp_commandline.html
http://www.faqs.org/docs/diveintopython/kgp_commandline.html
#3
2
The arguments are in sys.argv, the first one sys.argv[0]
is the script name.
参数在sys.argv中,第一个sys.argv [0]是脚本名称。
For more complicated argument parsing you should use argparse (for python >= 2.7). Previous modules for that purpose were getopts and optparse.
对于更复杂的参数解析,您应该使用argparse(对于python> = 2.7)。以前用于此目的的模块是getopts和optparse。
#4
1
Check these links
检查这些链接
http://www.faqs.org/docs/diveintopython/kgp_commandline.html
http://www.faqs.org/docs/diveintopython/kgp_commandline.html
Command Line Arguments In Python
Python中的命令行参数
#5
#6
0
it is also useful to determine option specific variables
确定选项特定变量也很有用
''' \
USAGE: python script.py -i1 input1 -i2 input2
-i1 input1 : input1 variable
-i2 input2 : input2 variable
'''
import sys
...
in_arr = sys.argv
if '-i1' not in in_arr or '-i2' not in in_arr:
print (__doc__)
raise NameError('error: input options are not provided')
else:
inpu1 = in_arr[in_arr.index('-i1') + 1]
inpu2 = in_arr[in_arr.index('-i2') + 1]
...
# python script.py -i1 Input1 -i2 Input2