65

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)

ofer.sheffer
  • 5,417
  • 7
  • 25
  • 26

7 Answers7

64

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)
Illarion Kovalchuk
  • 5,774
  • 8
  • 42
  • 54
  • What if I have a string like : ma'am which has an apostrophe(') in the middle? – dorado Feb 27 '16 at 08:16
  • 5
    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... – FuzzyAmi Jun 28 '17 at 06:10
  • Passing double quotes around file path with spaces threw the same error in Windows. This workaround worked like charm. – Rajaraman Subramanian Jul 25 '18 at 14:15
  • 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. – user3071170 Nov 09 '18 at 04:45
  • 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. – Ubuesque Feb 28 '21 at 20:40
48

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.

ofer.sheffer
  • 5,417
  • 7
  • 25
  • 26
  • As we usually read around here... feel free to accept your answer :D – Jblasco Aug 10 '13 at 00:15
  • 21
    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. – abarnert Aug 10 '13 at 00:26
  • 21
    Quotation doesn't help in my case, I still get error: unrecognized arguments: – Illarion Kovalchuk Nov 13 '14 at 13:58
  • @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? – ofer.sheffer Nov 17 '14 at 07:08
  • 1
    @ofer.sheffer, in my case the arguments are generated, and all things happen in linux on server side, so no copy paste. – Illarion Kovalchuk Nov 18 '14 at 09:00
  • 2
    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 Jul 24 '20 at 18:55
  • 1
    you probably forgot to pass $ARGS with quotes: yourpythonscript "$ARGS" – Mickael Feb 01 '22 at 10:19
  • Your solution is a miracle, it saved my week. – Ed J Mar 13 '23 at 14:22
12

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.

Uwe Brandt
  • 341
  • 2
  • 8
  • python 3.5.1 on RHEL6 – Uwe Brandt Apr 04 '16 at 09:12
  • 2
    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" – Brian C Jul 24 '20 at 18:58
2

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"

Dmitry
  • 536
  • 6
  • 10
1

A common mistake, when forwarding bash script arguments, is to forget double quotes. for example writting this:

ARGS="C:\SMTHNG\Name with spaces\MORE\file.csv"
mypythonscript  -d $ARGS

while it should be

ARGS="C:\SMTHNG\Name with spaces\MORE\file.csv"
mypythonscript -d "$ARGS"
Mickael
  • 301
  • 1
  • 13
0

There are 2 important points here (from my perspective):

  1. You do not want to replace all the spaces in the argument input.
  2. You want to use argparse interface.

My best aproax would be to use an argparse.Action with the function strip for the string:

import argparse

class StripArgument(argparse.Action):
    def __call__(self, parser, namespace, values, option_string=None):
        setattr(namespace, self.dest, values.strip())

parser = argparse.ArgumentParser(
    prog=f"your program", description=__doc__,
    formatter_class=argparse.RawDescriptionHelpFormatter,
    epilog="See '<command> --help' to read about a specific sub-command.")
parser.add_argument(
    "-n", "--variable-name", type=str, default='vx', action=StripArgument,
    help="Variable name inside something (default: %(default)s)")
BorjaEst
  • 390
  • 2
  • 11
-4

You need to surround your path with quotes such as:

python programname.py -path "c:\My path with spaces"

In the argument parse you get a list with one element. You then have to read it like:

path = args.path[0]
Dim
  • 1
  • 1