output of shell command

Sheila King sheila at spamcop.net
Mon Feb 26 02:15:34 EST 2001


On Mon, 26 Feb 2001 17:49:01 +1100, Joe Connellan <joe at al.com.au> wrote in
comp.lang.python in article <3A99FC5D.158A407B at al.com.au>:

:How do I retrieve the output of a shell command in python?
:
:eg
:
:output = os.system('ls -l') - but returns the output of "ls -l"
:or
:output = `ls -l` - like perl

Try this:

>>> import commands
>>> output=commands.getoutput("ls -l")
>>> print output
total 16
drwxr-xr-x   2 thinker  xthinksp     1024 Jan 30 01:49 Procmail
drwxr-xr-x   2 thinker  xthinksp     1024 Jan 14 13:45 ProgramTesting
-rw-r--r--   1 thinker  xthinksp     1460 Jan 21 01:51 environ.txt
-rw-r--r--   1 thinker  xthinksp      300 Jan  7 04:57 filelist.py
-rwxr-xr-x   1 thinker  xthinksp     5223 Dec  2 00:24 hello
-rw-r--r--   1 thinker  xthinksp      152 Dec  2 00:24 hello.cpp
drwxrwx---   6 thinker  xthinksp     1024 Jan  7 19:54 k12math_rejected
-rw-r--r--   1 thinker  xthinksp      602 Jan  7 02:09 lll.py
drwxr-xr-x   7 thinker  xthinksp     1024 Feb 15 21:10 mailreader
drwxr-xr-x   2 thinker  xthinksp     1024 Feb 28  2000 math508proj
>>>                                                                            


HTH

--
Sheila King
http://www.thinkspot.net/sheila/
http://www.k12groups.org/




More information about the Python-list mailing list