from sys import argv # 从Python的特性库中引入argv特性到自己的脚本中 # read the WYSS section for how to run this script, first, second, third = argv # 解包argv,并依次赋值给左边的变量 print("The script is called:", script) print("You first variables is:", first) print("Your second variables is:", second) print("Your third variables is:", third) # 运行命令 # Python ex13.py 1st 2 03 # 相当于依次赋值了4个值给argv左边
在Powershell中运行报错
python ex13.py Traceback (most recent call last): File "E:\python3\di1gexiangmu20231219\ex13.py", line 14, in <module> script, first, second, third = argv ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ValueError: not enough values to unpack (expected 4, got 1)
运行时同时按变量个数对应赋值
python ex13.py 1st 2 03
正确运行,结果为
The script is called: ex13.py You first variables is: 1st Your second variables is: 2 Your third variables is: 03
标签:third,script,variables,argv,second,first From: https://www.cnblogs.com/LeoCathyFelix/p/17930661.html