Redirecting system output quietly

Harry George harry.g.george at boeing.com
Tue Nov 16 09:21:48 EST 2004


Madhusudhanan Chandrasekaran <mc79 at cse.buffalo.edu> writes:

> Hi,
>   I am a newbie here. I want to know how to redirect os.system("ls -af")
> to a string variable without it barfing the answer of ls -af on the
> screen.
> 
> Thanks in advance,
> Madhusudhanan.C

1. If all you want is the list of files:
   myfiles=os.listdir('.')

2. If you want exactly the ls commands output, simplest is:
   import commands
   result=commands.getoutput("ls -af")

3. If you want more control, you may need forking and piping.
   Let me know if you need examples.

-- 
harry.g.george at boeing.com
6-6M21 BCA CompArch Design Engineering
Phone: (425) 294-4718



More information about the Python-list mailing list