By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,907 Members | 2,039 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,907 IT Pros & Developers. It's quick & easy.

execv putting quotes around arguments

P: n/a
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.

Here's an example:

Python 2.2.2 (#1, Jan 30 2003, 21:26:22)
[GCC 2.96 20000731 (Red Hat Linux 7.3 2.96-112)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
import os
arg0='/bin/ls'
arg1='-l foo'
args=[arg0]+[arg1]
os.execv(arg0, args)

/bin/ls: invalid option --
Try `/bin/ls --help' for more information.

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.

Mike Vieths

Jul 18 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
In article <40***********************@newsreader.visi.com>,
Mike Vieths <fo*****@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, do**@u.washington.edu
Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.