Python as a scripting language. Alternative to bash script?

Chris Rebert clp2 at rebertia.com
Tue Jul 6 20:27:57 EDT 2010


On Tue, Jul 6, 2010 at 1:35 PM, member thudfoo <thudfoo at opensuse.us> wrote:
> On Tue, Jul 6, 2010 at 6:40 AM, Michael Torrie <torriem at gmail.com> wrote:
>> On 07/06/2010 04:12 AM, sturlamolden wrote:
>>> On 28 Jun, 19:39, Michael Torrie <torr... at gmail.com> wrote:
>>>> In python I could simply take the output of "ps ax" and use python's
>>>> own, superior, cutting routines (using my module):
>>>>
>>>> (err, stdout, stderr) = runcmd.run( [ 'ps', 'ax' ] )
>>>> for x in stdout.split('\n'):
>>>>     print x.strip().split()[0]
>>>
>>> Or you just pass the stdout of one command as stdin to another. That
>>> is equivalent of piping with bash.
>>
>> Consider this contrived example:
>>
>> tail -f /var/log/messages | grep openvpn
>>
>> While it's possible to set up pipes and spawn programs in parallel to
>> operate on the pipes, in practice it's simpler to tell subprocess.Popen
>> to use a shell and then just rely on Bash's very nice syntax for setting
>> up the pipeline.  Then just read the final output in python.  If you set
>> the stdout descriptor to non-blocking, you could read output as it came.
>
> Is this a discussion about the pipes module in the std library?	

No, though that module is not irrelevant to Mr. Torrie's argument.

Cheers,
Chris
--
http://blog.rebertia.com



More information about the Python-list mailing list