Search code examples
pythonzsh

"zsh: unknown file attribute: 0" passing a Python tuple on the command line


I am calling a Python (3.8) script from Terminal on Mac using zsh (5.8) leading to the error message in the title. I found a workaround for this problem (albeit not elegant), but I would like to understand what is going wrong.

My Python file 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))

Calling this script from command line using python test.py --test (0.4,0.3) results in this error message zsh: unknown file attribute: 0

What does that mean?


Explanation about literal_eval:

literal_eval takes a string like "(0.3,0.4)" and evaluates it as tuple s.t.

a = literal_eval("(0.3,0.4)")
type(a)
<class 'tuple'>

Solution

  • The best workaround would be to put the string in quotes, so you would call it like:

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

    The reason of the error is that zsh tries to use globbing magic to expand (0.4,0.3) into filenames, but of course since you don't intend to use it you don't have the syntax quite right, thus the error. Just use quotes ' or " for strings, and zsh won't try to parse the argument, it will just pass the argument straight to the python command