os.system output

Steve Purcell stephen_purcell at yahoo.com
Wed Mar 21 11:50:17 EST 2001


Jonathan Soons wrote:
> I could do
> 
> >>>os.system('ls > file')
> >>>f = open('file')
> >>>dirs = f.readlines()
> 
> but it looks crude. Is there a better way?

Use the function 'popen' in the 'os' module.

>>> import os
>>> f = os.popen('ls')
>>> dirs = f.readlines()
>>> f.close()

Better still, for what you're trying to do:

>>> import os
>>> dirs = filter(os.path.isdir, os.listdir('.'))

-Steve

-- 
Steve Purcell, Pythangelist
Get testing at http://pyunit.sourceforge.net/
Any opinions expressed herein are my own and not necessarily those of Yahoo




More information about the Python-list mailing list