directory path types with argparse

前端 未结 3 1222
盖世英雄少女心
盖世英雄少女心 2020-12-13 08:35

My python script needs to read files from a directory passed on the command line. I have defined a readable_dir type as below to be used with argparse for validating that th

3条回答
  •  没有蜡笔的小新
    2020-12-13 08:55

    If your script can't work without a valid launch_directory then it should be made a mandatory argument:

    parser.add_argument('launch_directory', type=readable_dir)
    

    btw, you should use argparse.ArgumentTypeError instead of Exception in readable_dir().

提交回复
热议问题