Python中读取带有特殊字符的参数时出现问题

发布于 2024-11-30 17:33:13 字数 337 浏览 1 评论 0原文

我有一个脚本(a.py)读取两个这样的参数:-

#!/usr/bin/env python

import sys
username = sys.argv[1]
password = sys.argv[2]

问题是,当我使用一些特殊字符调用脚本时:-

a.py   "Lionel"   "my*password"

它给了我这个错误:-

/swdev/tools/python/current/linux64/bin/python: No match.

有解决方法吗?

I have a scripts (a.py) reads in 2 parameters like this:-

#!/usr/bin/env python

import sys
username = sys.argv[1]
password = sys.argv[2]

Problem is, when I call the script with some special characters:-

a.py   "Lionel"   "my*password"

It gives me this error:-

/swdev/tools/python/current/linux64/bin/python: No match.

Any workaround for this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你没皮卡萌 2024-12-07 17:33:13

问题在于您实际使用的命令与您向我们展示的命令不同。证据:在 Perl 中,前两个命令行参数是 $ARGV[0]$ARGV[1] (命令名称是 $0 >)。您向我们展示的 Perl 脚本不会产生您向我们展示的输出。

“不匹配”是 shell 错误消息。

复制并粘贴(不要重新输入)Python 脚本的准确内容、用于调用它的准确命令行以及您得到的确切输出。

还有一些需要注意的事情:

您将脚本作为 a.py 调用,这意味着您将其复制到 $PATH 中的某个目录,或者 . 在您的 $PATH 中。如果是后者,那是个坏主意;考虑一下如果您cd info 包含名为ls(可能是恶意)命令的目录,会发生什么情况。将 . 放在 $PATH 的末尾比将其放在开头更安全,但我仍然建议完全忽略它并使用 ./command 调用当前目录中的命令。无论如何,出于本练习的目的,请使用 ./a.py 而不是 a.py,这样我们就可以确保您不会选择另一个来自 $PATH 中其他位置的 a.py

这是一个不太可能的情况,但请检查当前目录中是否有名称中包含 * 字符的文件。如果没有匹配的文件,some_command asd*123(不带引号)将会失败,但如果碰巧有一个文件名实际上是"asd*123",则不会失败。

另一件要尝试的事情是:按如下方式更改您的 Python 脚本:

#!/usr/bin/env python

print "before import sys"

import sys

print "after import sys"

username = sys.argv[1]
password = sys.argv[2]

这将告诉您 shell 是否正在调用您的脚本。

The problem is in the commands you're actually using, which are not the same as the commands you've shown us. Evidence: in Perl, the first two command-line arguments are $ARGV[0] and $ARGV[1] (the command name is $0). The Perl script you showed us wouldn't produce the output you showed us.

"No match" is a shell error message.

Copy-and-paste (don't re-type) the exact contents of your Python script, the exact command line you used to invoke it, and the exact output you got.

Some more things to watch out for:

You're invoking the script as a.py, which implies either that you're copying it to some directory in your $PATH, or that . is in your $PATH. If the latter, that's a bad idea; consider what happens if you cd info a directory that contains a (possibly malicious) command called ls. Putting . at the end of your $PATH is safer than putting it at the beginning, but I still recommend leaving it out altogether and using ./command to invoke commands in the current directory. In any case, for purposes of this exercise, please use ./a.py rather than a.py, just so we can be sure you're not picking up another a.py from elsewhere in your $PATH.

This is a long shot, but check whether you have any files in your current directory with a * character in their names. some_command asd*123 (without quotation marks) will fail if there are no matching files, but not if there happens to be a file whose name is literally "asd*123".

Another thing to try: change your Python script as follows:

#!/usr/bin/env python

print "before import sys"

import sys

print "after import sys"

username = sys.argv[1]
password = sys.argv[2]

This will tell you whether the shell is invoking your script at all.

回忆那么伤 2024-12-07 17:33:13

该错误来自您的 shell,而不是来自 Python。您是否在 .bashrc 或其他地方设置了 shopt -s failureglob

That error comes from your shell, not from Python. Do you have a shopt -s failglob set in your .bashrc or somewhere?

死开点丶别碍眼 2024-12-07 17:33:13

/swdev/tools/python/current/linux64/bin/python:不匹配。

我认为问题是 python 环境未设置:

Python 在你的机器上运行吗?

/swdev/tools/python/current/linux64/bin/python: No match.

I think the problem is that the python env is not set:

Does python run at all on your machine ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文