当我使用命令行参数时,为什么python将我的文件名作为一个值来计算?

2024-04-30 02:43:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经试着修复它,并且已经检查了我的代码很多次,但是看起来没有什么问题

我已将模块解压为3个值并尝试运行程序,但它给了我一个错误,即我给出的值太多:

PS C:\dir1>;python exlp.py拉面清爽圆润

回溯(最近一次呼叫):

文件“exlp.py”,第54行,in

ramen, lucid, chubby = argv

ValueError:要解包的值太多

过了一会儿,我发现它是把文件名作为一个值来计算的。我不知道为什么会这样,但这是我的代码:

  from sys import argv

sakura, ramen, lucid, chubby = argv

print "You love...", sakura
print "You like to eat...", ramen
print "Your dreams are...", lucid
print "You are...", Naruto

print raw_input('Do you know someone named Naruto?')

谢谢你


Tags: 模块代码py程序youareprintargv
1条回答
网友
1楼 · 发布于 2024-04-30 02:43:26

因为那是what it does

sys.argv

The list of command line arguments passed to a Python script. argv[0] is the script name (it is operating system dependent whether this is a full pathname or not). If the command was executed using the -c command line option to the interpreter, argv[0] is set to the string '-c'. If no script name was passed to the Python interpreter, argv[0] is the empty string.

To loop over the standard input, or the list of files given on the command line, see the fileinput module.

计算该值或切片该列表:

_, ramen, lucid, chubby = argv # option 1
ramen, lucid, chubby = argv[1:] # option 2

相关问题 更多 >