简体   繁体   English

无法在命令行中将多个参数传递给python脚本

[英]Fail to pass multiple arguments to a python script in command line

I was trying to write a python script like this: 我正在尝试编写这样的python脚本:

import sys

print sys.argv[1]
print sys.argv[2]

Let's call it arg.py , and run it in command line: 让我们将其arg.py ,然后在命令行中运行它:

python arg.py one two

it printed: one two. 它打印:一两个。

Everything was fine. 一切都很好。

Then I wanted it to be handy so I put arg.py in my $PATH and gave it permission to exacuate so wherever I'm I can simply type arg in command line to run this script. 然后,我希望它可以派上用场,所以我将arg.py放到$PATH并给予它撤消的权限,因此无论我在哪里,我都可以在命令行中键入arg来运行此脚本。 I tried 我试过了

arg one two

but it failed. 但是失败了。 The exception said:"bash: test: one: unary operator expected". 异常说:“ bash:测试:one:期望一元运算符”。 But if I just do 但是如果我只是做

arg one

it worked fine. 工作正常。

My question is: why I can't pass multiple arguments like this? 我的问题是:为什么我不能这样传递多个参数? And what is the right way? 正确的方法是什么?

Thanks! 谢谢!

You probably named your script test , which is a Bash builtin name. 您可能将脚本test命名为Bash内置名称。 Name it something else. 命名其他。

$ help test
test: test [expr]
    Evaluate conditional expression.

    Exits with a status of 0 (true) or 1 (false) depending on
    the evaluation of EXPR.  Expressions may be unary or binary.  Unary
    expressions are often used to examine the status of a file.  There
    are string operators and numeric comparison operators as well.

    The behavior of test depends on the number of arguments.  Read the
    bash manual page for the complete specification.

    ...

That's why you're getting the error from bash : 这就是为什么您从bash收到错误的原因:

bash: test: one: unary operator expected
                   ^--------- because it expects an operator to go before 'two'
             ^-------- and test doesn't like the argument 'one' you've provided
       ^-------- because it's interpreting your command as the builtin 'test'
  ^--- Bash is giving you an error

You should parse command line arguments in Python using argparse or the older optparse . 您应该使用argparse或更旧的optparse在Python中解析命令行参数。

Your script, as it is, should work. 您的脚本应该可以正常工作。 Remember to put a shebang line that tells bash to use Python as interpreter, eg #! /usr/bin/env python 切记放一个shebang行,告诉bash使用Python作为解释器,例如#! /usr/bin/env python #! /usr/bin/env python

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在python脚本功能内部的sql查询中传递多个命令行参数或系统参数? - How to pass multiple command line arguments or system arguments in sql query which is inside the function of python script? 将命令行参数传递给没有脚本文件的python解释器 - pass command line arguments to python interpreter without a script file 如何将参数传递给从命令行启动的 python gdb 脚本 - How to pass arguments to a python gdb script launched from command line Python 脚本 - 如何从命令行参数将密码作为变量传递 - Python script - how to pass password as variable from command line arguments 使用 sys.argv 将命令行 arguments 传递给 python 脚本 - Using sys.argv to pass command line arguments to a python script 将命令行参数传递给通过unix可执行文件调用的python脚本 - Pass command line arguments to python script that is called through unix executable 如何从脚本将命令行参数传递给python文件 - How to pass command line arguments to a python file from a script 使用多个命令行参数值运行 python 脚本 - Running python script with multiple values of command line arguments 如何从另一个脚本中将命令行 arguments 传递给 python 脚本? - How to pass command-line arguments to a python script from within another script? Python:将通用字典作为命令行参数传递 - Python: Pass a generic dictionary as a command line arguments
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM