Handle spaces in argparse input

后端 未结 5 1981
逝去的感伤
逝去的感伤 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:22

    After some experiments (python 2.7 Win10) I found out that the golden rule is to put quotes ("") around arguments which contain spaces and do NOT put if there are no spaces in argument. Even if you are passing a string/path. Also putting a single quotes ('') is a bad idea, at least for Windows.

    Small example: python script.py --path ....\Some_Folder\ --string "Here goes a string"

提交回复
热议问题