Handle spaces in argparse input

后端 未结 5 1982
逝去的感伤
逝去的感伤 2020-12-28 12:52

Using python and argparse, the user could input a file name with -d as the flag.

parser.add_argument(\"-d\", \"--dmp\", default=None)

Howev

5条回答
  •  甜味超标
    2020-12-28 13:39

    You need to surround your path with quotes such as:

    python programname.py -path "c:\My path with spaces"
    

    In the argument parse you get a list with one element. You then have to read it like:

    path = args.path[0]
    

提交回复
热议问题