python - Handle spaces in argparse input -


using python , argparse, user input file name -d flag.

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

however, failed when path included spaces. e.g.

-d c:\smthng\name spaces\more\file.csv 

note: spaces cause error (flag takes in 'c:smthng\name' input).

error: unrecognized arguments: spaces\more\file.csv 

took me longer should have find solution problem... (did not find q&a i'm making own post)

for can't parse arguments , still "error: unrecognized arguments:" found workaround:

parser.add_argument('-d', '--dmp', nargs='+', ...) opts = parser.parse_args() 

and when want use

' '.join(opts.dmp) 

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -