output of shell command

Emile van Sebille emile at fenx.com
Mon Feb 26 13:38:58 CET 2001


This works on my linux box and as Joe was looking for
'ls -l', I assume it'll work for him too.

But as I hadn't used the commands module before, I tried it
on both my Win95 and Win2k boxes, where it failed to get the
right info.  So, either I'm doing it wrong, or the
combination of commands and windows is broken, which I
suspect.

However, a=os.popen("dir").readlines() works on windows and
linux.  So if you're writing cross-platform, you'll want to
keep this in mind.

--

Emile van Sebille
emile at fenx.com
-------------------


"Sheila King" <sheila at spamcop.net> wrote in message
news:ci0k9tkke44ule7hlg2q7j6b8v5vsddlnk at 4ax.com...
> 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