Stop argparse from globbing filepath

冷暖自知 提交于 2019-12-01 20:06:16

问题


I am using python argparse with the following argument definition:

parser.add_argument('path', nargs=1, help='File path to process')

But when I enter my command with a wildcard argument, argparse globs all the file paths and terminates with an error.

How do I get argparse not to glob the files?


回答1:


How do I get argparse not to glob the files?

You don't.

You get the shell to stop globbing.

However. Let's think for a moment.

You're saying this in your code

parser.add_argument('path', nargs=1, help='File path to process')

But you are actually providing wild-cards when you run it.

One of those two is wrong. Either stop providing wild-cards at run time or fix argparse to allow multiple filenames.




回答2:


The shell is expanding the wildcard argument before argparse gets a chance to see it. Put quotes around the wildcard argument to prevent the shell from expanding it.

You could later perform the wildcard expansion with glob.glob.




回答3:


The globbing is done by your shell, not by the argparse module. print sys.argv at the beginning and you will see what argparse gets as input.



来源:https://stackoverflow.com/questions/7366791/stop-argparse-from-globbing-filepath

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!