python-arguments.py
from sys import argv name, first, second, third, fourth = argv print "Script name is:", name print "Your first argument is:", first print "Your second argument is:", second print "Your third argument is:", third print "Your fourth argument is:", fourth # Alternatively we can access "argv" argument list directly using range. For exmaple: # Print all arguments except script name print argv[1:] # Print second argument print argv[2] # Print second and third argument print argv[2:4] # Print last argument print argv[-1]
如果在执行时提供四个命令行参数,则上述脚本将生成以下输出:
$python python-arguments.py one two three four Script name is: python-arguments.py Your first argument is: one Your second argument is: two Your third argument is: three Your fourth argument is: four ['one', 'two', 'three', 'four'] two ['two', 'three'] four
日期:2020-07-07 20:54:35 来源:oir作者:oir