"zsh: unknown file attribute: 0" 在命令行上传递一个 Python 元组
"zsh: unknown file attribute: 0" passing a Python tuple on the command line
我在 Mac 上使用 zsh (5.8) 从终端调用 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)"
这样的字符串并将其计算为元组 s.t.
a = literal_eval("(0.3,0.4)")
type(a)
<class 'tuple'>
最好的解决方法是将字符串放在引号中,这样您就可以这样称呼它:
python test.py --test '(0.4,0.3)'
错误的原因是 zsh
试图使用 globbing 魔法将 (0.4,0.3)
扩展为文件名,但是当然因为您不打算使用它,所以您没有语法完全正确,因此错误。只需对字符串
使用引号 '
或 "
我在 Mac 上使用 zsh (5.8) 从终端调用 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)"
这样的字符串并将其计算为元组 s.t.
a = literal_eval("(0.3,0.4)")
type(a)
<class 'tuple'>
最好的解决方法是将字符串放在引号中,这样您就可以这样称呼它:
python test.py --test '(0.4,0.3)'
错误的原因是 zsh
试图使用 globbing 魔法将 (0.4,0.3)
扩展为文件名,但是当然因为您不打算使用它,所以您没有语法完全正确,因此错误。只需对字符串
'
或 "