Feed on
Posts
Comments

to execute a shell command in a python script, import the os module:

import os

and then use the popen4 function, which returns file objects corresponding to both stdin and stdout:

(stdin,stdout) = os.popen4(“ls”)

print stdout.read()


Bookmark and Share

if that was helpful ...

check out the other tips and tricks i've compiled on these pages. you might learn something else interesting!

2 Responses to “run a shell command in a python script”

  1. on 12 Jun 2007 at 5:50 am kougar

    Thanks for the information.
    It has been helpful for me.

  2. on 01 Oct 2007 at 2:07 pm Eric Brunson

    It’s an old comment, but os.popen*() is deprecated in favor of the subprocess module. Far more powerful and will be supported for the foreseeable future.

    There’s a very good Examples section in the library reference, your example is covered here: http://docs.python.org/lib/node535.html

Did I get this wrong? Let me know!

Trackback URI | Comments RSS