python3.7 optionparser input option with asterisk(*) becomes a file in the folder

93 views Asked by At

Env:

python3.7
OptionParser with a option [ add_option('-t', '--target', action='append', dest='targets') ]
OS: CentOS7.6

Problem:

So I am using this option to input a list of targets, and with this command line:
parser -t logs* -t test
there's a file "logs.tar.gz" in where I execute this command line, when i print the value of targets, this is what i get:
['logs.tar.gz', 'test']
So I believe this is a 'problem' of system level, and what I want to know is:
is there any way to make logs* be logs* without input logs\* in python?

1

There are 1 answers

1
rdas On BEST ANSWER

The shell is who is expanding the *. There is nothing that python can do here since it never gets to know about the log*.

You can force your shell to interpret the * as a literal value with some quoting:

parser -t "logs*" -t test

This works in zsh, it might be different for your shell.