<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Michael Williams wrote:
<blockquote cite="mid:E2BF4A0F-9984-4F7A-93EF-F82F30BAAEA6@mgreg.com"
type="cite">
<pre wrap="">Hi All,
I've recently seen the "subprocess" module and am rather confused by
it's requirements. Is it not possible to execute an entire string
without having to break them up into a list of arguments? For
instance, I'd much rather do the following:
subprocess.call("ls -al | grep -i test")
. . .than to have to:
list = ["ls", "-a", "-l" ] . . . . . . and so on and so forth.
subprocess.call(list. . .)
What is the best way to go about executing a massively complex single
line command?
Thanks,
Michael
</pre>
</blockquote>
How about a hybrid solution?:<br>
<blockquote>from subprocess import Popen, PIPE<br>
from string import strip<br>
(stdout, stderr) = Popen(['ls','-al'], stdout=PIPE,
stderr=PIPE).communicate()<br>
<i># process spins off. Ideally, you should wait for it to complete,
or assign the resulting object of Popen<br>
# to a variable and inquire if it's completed via the 'poll()' operator
or wait on it via the 'wait()' <br>
# operator.<br>
# then query the stderr via communicate()[1]; if good, i.e. len(stderr)
== 0, inspect the resulting <br>
# stdout string (via communicate()[0]) for your 'test' result. But I'm
being lazy. I assume the call to <br>
# ls -al will be faster then my next statement.</i><br>
res = []<br>
for line in stdout.strip():<br>
if 'test' in line:<br>
res.append( line )<br>
</blockquote>
<br>
Do the work you need done at the shell prompt, do
the rest in Python. Also I wonder if, you were looking for the word
'test' in the filename, glob.glob wouldn't have been a bit more
efficient? But if you were looking for a 'test' owner or group then
using:<br>
<blockquote>glob.glob(): get the <u>list</u> of files, with
directory. Example: <b>glob.glob('/bin/*') </b><br>
os.stat(): get an <u>object</u> of stats on a file. Example:<b>
os.stat('/bin/ls')</b><br>
stat.ST_UID: get just the group id, as an <u>integer</u> from the
object returned by os.stat. Example: <b>os.stat('/bin/ls')[stat.ST_UID]<br>
</b>pwd.getpwgid(): get the <u>string</u> translation of a UID.
Example: <br>
<b>pwd.getpwgid( os.stat('/bin/ls')[stat.ST_UID] )</b><br>
stat.ST_GID: get the group id, as an <u>integer</u> from the object
returned os.stat. Example:<br>
<b>os.stat('/bin/ls')[stat.ST_GID]</b><br>
grp.getgrgid(): get the <u>string</u> translation of a UID. Example: <br>
<b>grp.getgrgid( os.stat('/bin/ls')[stat.ST_UID] )</b></blockquote>
Now I have a list, which I iterate over, getting the UID and GID, as
strings, that I can compare to. I never had to use a subprocess and
having to build up a 'wait on done loop'. Note, grp and pwd, I think,
are
Python/Unix functions. What one does on Windows I don't know, but I'll
bet there are similar functions under Python/Windows. Don't get me
wrong, Subprocess is a fast and damn useful tool. I often use it to
verify a program is in my path, before creating a subprocess to run
some task python isn't built for (like mencoder functionality).<br>
<br>
You see, the issue is: <u>shell,</u> <u>python & shell</u> or <u>python
alone</u>.
When transitioning from one language to another, say C/C++/Java to
Python, it is often easier to use your knowledge of the shell command
(bash/Bourne for example) to get things done. But it's only a
transitional issue. The need to use the shell is a good demonstrator
that either the Interpretor is weak, or that you haven't fully explored
it's abilities. With the Python Interpretor at v2.4 going onto v2.5,
it's very likely that the desired feature exists; you have but to do
some clever research to find it. As to clever research ... I find
Google a good place to start.<br>
<br>
I had some familiarity with os.stat and glob. But the grp and pwd
functions were things I discovered from
<a class="moz-txt-link-freetext" href="http://docs.python.org/modindex.html">http://docs.python.org/modindex.html</a> and about two minutes of searching
for 'python uid to name' @ Google.<br>
<br>
Also, os.listdir and os.path.join would have worked just as well as
glob.glob('dirname/*'). But glob.glob was faster to write. Even a
better choice would have been to use the 'os.walk' to iterate over the
directory tree.<br>
<br>
sph.<br>
<br>
<pre class="moz-signature" cols="72">--
HEX: 09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0
</pre>
</body>
</html>