Handle spaces in argparse input
Solution 1
Simple solution: argparse considers a space filled string as a single argument if it is encapsulated by quotation marks.
This input worked and "solved" the problem:
-d "C:\SMTHNG\Name with spaces\MORE\file.csv"
NOTICE: argument has "" around it.
Solution 2
For those who can't parse arguments and still get "error: unrecognized arguments:" I found a workaround:
parser.add_argument('-d', '--dmp', nargs='+', ...)
opts = parser.parse_args()
and then when you want to use it just do
' '.join(opts.dmp)
Solution 3
Bumped into this problem today too.
-d "foo bar"
didn't help. I had to add the equal sign
-d="foo bar"
and then it did work.
Solution 4
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"
Related videos on Youtube
ofer.sheffer
Updated on March 22, 2022Comments
-
ofer.sheffer about 2 years
Using python and argparse, the user could input a file name with -d as the flag.
parser.add_argument("-d", "--dmp", default=None)
However, this failed when the path included spaces. E.g.
-d C:\SMTHNG\Name with spaces\MORE\file.csv
NOTE: the spaces would cause an error (flag only takes in 'C:SMTHNG\Name' as input).
error: unrecognized arguments: with spaces\MORE\file.csv
Took me longer than it should have to find the solution to this problem... (did not find a Q&A for it so I'm making my own post)
-
Jblasco almost 11 yearsAs we usually read around here... feel free to accept your answer :D
-
abarnert almost 11 yearsThis isn't about argparse; this is how shells parse arguments on most systems (and how programs fake shell-style-parsing on the systems that don't have real shells). By the time you get to the
argparse
module, your original version is already 4 separate arguments, and argparse can't do anything about that. -
Illarion Kovalchuk over 9 yearsQuotation doesn't help in my case, I still get error: unrecognized arguments:
-
ofer.sheffer over 9 years@Shaman, just throwing a guess out there -- if you copy-pasted the arguments, they might be the wrong unicode. “ is different than ". Other than that, more input from you might help. Does a print(argv) work?
-
Illarion Kovalchuk over 9 years@ofer.sheffer, in my case the arguments are generated, and all things happen in linux on server side, so no copy paste.
-
dorado about 8 yearsWhat if I have a string like : ma'am which has an apostrophe(') in the middle?
-
Uwe Brandt about 8 yearspython 3.5.1 on RHEL6
-
FuzzyAmi almost 7 yearsSo to be honest I dont even recall writing this comment. But I think this answer is better than the accepted one because it doesn't assume anything about the shell. the accepted answer isn't about argparse - its about shell. and it appears to not work for every kind of shell out there (as noted in the comments). plus, this answer outvoted the accepted one...
-
Rajaraman Subramanian almost 6 yearsPassing double quotes around file path with spaces threw the same error in Windows. This workaround worked like charm.
-
user3071170 over 5 yearsIf such an optional argument is specified before the positional arguments on the command line (which is a POSIX guideline) then the optional argument will consume all the positional arguments immediately following it.
-
Brian C almost 4 yearsThis did not work for me either. ARGS="--datasourceFile \"../datasources/CI-CD Test Cube.smodel\"" echo $ARGS then run result: --datasourceFile "../datasources/CI-CD Test Cube.smodel" error: unrecognized arguments: Test Cube.smodel"
-
Brian C almost 4 yearsThis did not work for me either. my param looks like: ARGS="--datasourceFile=\"../datasources/CI-CD Test Cube.smodel\"" which echod looks like: --datasourceFile="../datasources/CI-CD Test Cube.smodel" and I get error: unrecognized arguments: Test Cube.smodel"
-
Ubuesque about 3 yearsThis is just a workaround, not a definitive answer, for example if the string contains multiple consecutive characters (e.g.
ex ample
), this method will not work. -
Mickael over 2 yearsyou probably forgot to pass $ARGS with quotes: yourpythonscript "$ARGS"