running a shell command from a python program

S

Sandman

Hi,
I'm a newbie, so please be gentle :)

How would I run a shell command in Python?

Here is what I want to do:
I want to run a shell command that outputs some stuff, save it into a
list and do stuff with the contents of that list.

I started with a BASH script actually, until I realized I really needed
better data structures :)

Is popen the answer? Also, where online would I get access to good
sample code that I could peruse?

I'm running 2.2.3 on Linux, and going strictly by online doc so far.

Thanks!
S C
 
T

Thomas Guettler

Am Wed, 23 Feb 2005 07:00:31 -0800 schrieb Sandman:
Hi,
I'm a newbie, so please be gentle :)

How would I run a shell command in Python? [cut]
Is popen the answer? Also, where online would I get access to good
sample code that I could peruse?

Yes, popen is the answer. I recommend popen4 because it avoids
deadlocks if there is output on stdout and stderr.

Example:
stdout, stdin = popen2.popen4("tidy -q -errors '%s'" % htmlfile)

HTH,
Thomas
 
A

aurora

In Python 2.4, use the new subprocess module for this. It subsume the
popen* methods.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,436
Messages
2,571,696
Members
48,796
Latest member
Greg L.
Top