1

I have been using the argparse module for accepting input into a program, with much success. I have defined about six optional arguments (arguments that need to be prepended with a -- can be placed anywhere in the list) and one positional argument (required arguments that use the order placed to determine the argument), so my usage chart looks like this (with extra optional arguments removed for brevity):

usage: Main.py [-h] [--site SITE] [--username USERNAME] [--password PASSWORD]
           [--verbose]
           filepath

In order to run a Python 2.7 script on Debian (which ships with Python 2.6), I have had success using pythonbrew, which allows multiple Python installations to co-exist on the same server. A nice side effect of using it means that I can turn off executable permissions on my script, which makes user management much easier for me.

All was well, until I tried to store to a filepath (my lone positional argument) that had a space in it, and I found that I could not!

pythonbrew py Main.py --verbose /media/rackspace/db1/clients/My\ Client/
pythonbrew py Main.py --verbose '/media/rackspace/db1/clients/My\ Client/'
pythonbrew py Main.py --verbose "/media/rackspace/db1/clients/My\ Client/"
pythonbrew py Main.py --verbose [/media/rackspace/db1/clients/My\ Client/]

pythonbrew py Main.py --verbose /media/rackspace/db1/clients/My Client/
pythonbrew py Main.py --verbose '/media/rackspace/db1/clients/My Client/'
pythonbrew py Main.py --verbose "/media/rackspace/db1/clients/My Client/"
pythonbrew py Main.py --verbose [/media/rackspace/db1/clients/My Client/]

all return the same error message

Main.py: error: unrecognized arguments: Client

Can anyone confirm this error? The fact that no one has mentioned it yet in the Python groups makes me believe that this is a Pythonbrew bug, but I am hoping to keep Pythonbrew and find an alternate way to run this script. For now, I am simply storing the files to a folder without a space, but I am hoping that someone can help with this error.

Simon Hova
  • 293
  • 2
  • 12

2 Answers2

2

The problem is in pythonbrew/commands/py.py. Each call to subprocess.Popen() needs to be modified so that it uses a list for the command instead of a string.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • I've had success before using [shlex.split](http://docs.python.org/library/shlex.html) to get the needed list from a string command – Johnny Brown Jun 27 '11 at 16:38
  • You may be onto something with that answer. When I combine my examples (don't know why I didn't) and use `pythonbrew py Main.py --verbose ['/media/rackspace/db1/clients/My Client/']`, it passes properly. But, in a golden bit of irony, it passes the argument with the brackets built in. So, my code fails with **No such directory: '[/media/rackspace/db1/clients/My Client/]'**. Is there another way to pass the list? – Simon Hova Jun 27 '11 at 16:39
  • Why would you do that instead of doing as my answer says? – Ignacio Vazquez-Abrams Jun 27 '11 at 16:46
  • @Ignacio I thought that I was doing what your answer said. I read it as instead of using a string (**'/media/rackspace/db1/clients/My\ Client/'**) for my call, I should use a list (**['/media/rackspace/db1/clients/My Client/']**). I humbly apologize if I misunderstood. Can you try to explain it again? btw: This command is being run from the command line, so the code more accurately looks like: `$ pythonbrew py Main.py --verbose [/media/rackspace/db1/clients/My\ Client/]`. I don't use the subprocess module at all. – Simon Hova Jun 27 '11 at 16:58
  • 1
    Got it. I've [submitted](https://github.com/utahta/pythonbrew/issues/30) the issue to the GitHub repo for Pythonbrew, and included a link to this question. Hopefully the problem can be resolved. Thanks again! – Simon Hova Jun 27 '11 at 17:23
1

I can not post a comment since I am too new, but have you tried to escape the space, by placing a \ in front of it? Maybe something like :

pythonbrew py Main.py --verbose /media/rackspace/db1/clients/My\ Client/

Senthess
  • 17,020
  • 5
  • 23
  • 28