Using "subprocess" without lists. . .?

Steven Howe howe.steven at gmail.com
Sun May 13 14:17:34 EDT 2007


Michael Williams wrote:
> 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
>   
How about a hybrid solution?:

    from subprocess import Popen, PIPE
    from string import strip
    (stdout, stderr) = Popen(['ls','-al'], stdout=PIPE,
    stderr=PIPE).communicate()
    /# process spins off. Ideally, you should wait for it to complete,
    or assign the resulting object of Popen
    # to a variable and inquire if it's completed via the 'poll()'
    operator or wait on it via the 'wait()'
    # operator.
    # then query the stderr via communicate()[1]; if good, i.e.
    len(stderr) == 0, inspect the resulting
    # stdout string (via communicate()[0]) for your 'test' result. But
    I'm being lazy. I assume the call to
    # ls -al will be faster then my next statement./
    res = []
    for line in stdout.strip():
        if 'test' in line:
           res.append( line )


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:

    glob.glob(): get the _list_ of  files, with directory. Example: 
    *glob.glob('/bin/*') *
    os.stat(): get an _object_ of stats on a file. Example:*
    os.stat('/bin/ls')*
    stat.ST_UID: get just the group id, as an _integer_ from the object
    returned by os.stat. Example: *os.stat('/bin/ls')[stat.ST_UID]
    *pwd.getpwgid(): get the _string_ translation of a UID. Example:
    *pwd.getpwgid( os.stat('/bin/ls')[stat.ST_UID] )*
    stat.ST_GID: get the group id, as an _integer_ from the object
    returned os.stat. Example:
    *os.stat('/bin/ls')[stat.ST_GID]*
    grp.getgrgid(): get the _string_ translation of a UID. Example:
    *grp.getgrgid( os.stat('/bin/ls')[stat.ST_UID] )*

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).

You see, the issue is: _shell,_ _python & shell_ or _python alone_. 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.

I had some familiarity with os.stat and glob. But the grp and pwd 
functions were things I discovered from 
http://docs.python.org/modindex.html and about two minutes of searching 
for 'python uid to name' @ Google.

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.

sph.

-- 
HEX: 09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20070513/17dc5b6d/attachment.html>


More information about the Python-list mailing list