Warm tip: This article is reproduced from serverfault.com, please click

其他-“ zsh:未知文件属性:0”在命令行上传递Python元组

(其他 - "zsh: unknown file attribute: 0" passing a Python tuple on the command line)

发布于 2020-12-04 15:55:30

我正在Mac上使用zsh(5.8)从Ma​​c上的Terminal调用Python(3.8)脚本,导致标题中出现错误消息。我找到了解决此问题的方法(尽管不是很好),但是我想了解出了什么问题。

我的Python文件test.py

import argparse
from ast import literal_eval

parser = argparse.ArgumentParser(description="test")
parser.add_argument("--test", default="", type=str, help="test")

args = parser.parse_args()

print(literal_eval(args.test))

从命令行使用python test.py --test (0.4,0.3)此错误消息中的结果调用此脚本zsh: unknown file attribute: 0

这意味着什么?


有关literal_eval以下内容的说明

literal_eval接受像这样的字符串"(0.3,0.4)"并将其评估为元组st

a = literal_eval("(0.3,0.4)")
type(a)
<class 'tuple'>
Questioner
Carol Eisen
Viewed
12
Alexey Larionov 2020-12-05 00:01:26

最好的解决方法是将字符串放在引号中,因此你可以这样称呼它:

python test.py --test '(0.4,0.3)'

错误的原因是zsh尝试使用通配符魔术来扩展(0.4,0.3)为文件名,但是当然,由于你不打算使用它,因此语法不正确,因此是错误的。只需使用引号'"字符串