-1
import argparse

if __name__=='__main__':
    parser= argparse.ArgumentParser()
    parser.add_argument('--template')

    args= parser.parse_args()

    print(args.template)

above_file.py --template sub-*/my/file.txt

reads all files described by the pattern, not the string sub-*/my/file.txt. I would like to read the string only. I found a way around by enclosing the value within " ". But I am wondering if something could be done inside add_argument to prevent that from happening.

tash
  • 711
  • 5
  • 13
  • 1
    argparse can't do anything about it. It's the shell interpreting that argument before it's even passed to your Python program. That's how shells work. Enclosing the argument in quotes is exactly what you must do to prevent the shell from doing that. – deceze Nov 20 '20 at 16:14
  • Thank you, I understand. – tash Nov 20 '20 at 16:29
  • or use a windows platform, no wildcard expansion by terminal – rioV8 Nov 20 '20 at 16:34

1 Answers1

1

Unfortunately, it cannot be done. The pattern is expanded by your shell, from within your python script you cannot know whether your script was called like

python above_file.py --template sub-*/my/file.txt

or

python above_file.py --template sub-1/my/file.txt sub-2/my/file.txt sub-3/my/file.txt

What you can do on the shell: Stop shell wildcard character expansion?