Handle spaces in argparse input

45,865

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"

Share:
45,865

Related videos on Youtube

ofer.sheffer
Author by

ofer.sheffer

Updated on March 22, 2022

Comments

  • ofer.sheffer
    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
    Jblasco almost 11 years
    As we usually read around here... feel free to accept your answer :D
  • abarnert
    abarnert almost 11 years
    This 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
    Illarion Kovalchuk over 9 years
    Quotation doesn't help in my case, I still get error: unrecognized arguments:
  • ofer.sheffer
    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
    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
    dorado about 8 years
    What if I have a string like : ma'am which has an apostrophe(') in the middle?
  • Uwe Brandt
    Uwe Brandt about 8 years
    python 3.5.1 on RHEL6
  • FuzzyAmi
    FuzzyAmi almost 7 years
    So 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
    Rajaraman Subramanian almost 6 years
    Passing double quotes around file path with spaces threw the same error in Windows. This workaround worked like charm.
  • user3071170
    user3071170 over 5 years
    If 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
    Brian C almost 4 years
    This 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
    Brian C almost 4 years
    This 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
    Ubuesque about 3 years
    This 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
    Mickael over 2 years
    you probably forgot to pass $ARGS with quotes: yourpythonscript "$ARGS"