[Tutor] os.system() with imbeded quotes
Kushal Kumaran
kushal.kumaran+python at gmail.com
Thu Apr 4 19:11:14 CEST 2013
cevyn e <cevyne at gmail.com> writes:
> I get the example os.system('ls -al') no problem.
>
> i'm trying to create a variable with my command built but needs to include
> quotes.
> Portion of code is as follows:
> someip = '192.168.01.01'
>
> var1 = 'lynx -dump http://' + someip +
> '/cgi-bin/xxxx.log&.submit=+++Go%21+++ > junk'
>
> print var1
>
> os.system(var1)
>
>
> If I print var1 it looks right . If I use the os.system(var1) as above it
> seems to have a problem near the end of the string with msg
> sh: .submit=+++Go%21+++: command not found
>
> clearly there is some escape sequence that I don't understand .
>
> I tried combinations of single and double quotes and mixed around var1, but
> that generates command not found.
>
> I need it to look like how I enter it manually and works
> lynx -dump 'http://192.168.01.01/cgi-bin/xxxx.log&.submit=+++Go%21+++ >
> junk'
>
> Probably obvious to many but i'm spinning my wheels. many thanks for help .
There's a pipes.quote function (in python2, available as shlex.quote in
python3.3) that you can use in these circumstances. Your problem is
because os.system passes the command to a shell, and the '&' character
in your argument terminates the command line.
Here's a transcript that shows the use of pipes.quote:
In [1]: import os
In [2]: os.system('echo a&b')
sh: 1: b: not found
a
Out[2]: 32512
In [5]: os.system('echo %s' % (pipes.quote('a&b'),))
a&b
Out[5]: 0
You should be able to get the effect you want by doing this:
cmd = 'lynx -dump %s > junk' % (pipes.quote(url),)
That said, you should take a look at the subprocess module. The
functions in that module can invoke commands without invoking a shell,
which avoids such problems.
Untested example:
with open('junk', 'wb') as output_stream:
p = subprocess.Popen(['lynx', '-dump', url], stdout=output_stream)
p.communicate()
In this case, no quoting of the "url" argument is required.
Finally, you can also look at higher level ways of doing whatever it is
you're trying. See the urllib2 standard module (assuming python2
because of your print statement), or the requests module
(http://docs.python-requests.org/en/v1.1.0/) which provide facilities to
access different kinds of urls with varying degrees of automation.
--
regards,
kushal
More information about the Tutor
mailing list