execv putting quotes around arguments

Donn Cave donn at u.washington.edu
Fri Feb 6 12:27:44 EST 2004


In article <4023c273$0$41295$a1866201 at newsreader.visi.com>,
 Mike Vieths <foeclan at yahoo.com> wrote:
> I'm running into a problem when I try to run commands with os.execv.  It 
> seems to be putting quotation marks around each element of the list 
> passed as its second argument.  This is fine for the most part, but if 
> the argument has a space in it, getopt (in the command being called) 
> will read the entire quoted string as the argument and generally fail.
...
> This can be worked around by breaking '-l foo' into two seperate 
> elements ('-l' and 'foo'), but that's not always intuitive.  Anyone know 
> why those quotes are there, and if there's a way to make them go away? 
> I'm stuck with execv, since this is part of a larger project for which 
> I'm creating a module, and modifying that portion to use os.popen, 
> os.system, or something similar isn't an option.

os.system(cmdline) is about the same as os.fork()
followed by os.execv('/bin/sh', ['sh', '-c', cmdline])

The way execv works is generally a strong advantage,
because it doesn't automatically run the shell over
its parameters (it doesn't `add quotes', it just
doesn't need them), and that shell step opens you
up to all kinds of unexpected possibilities if you
don't positively know what's going into the command
line.  But if you want the shell, you can have it -
just invoke it.

   Donn Cave, donn at u.washington.edu



More information about the Python-list mailing list