ouput redirection..a newbie question
Jp Calderone
exarkun at intarweb.us
Mon Dec 15 09:21:53 EST 2003
On Tue, Dec 18, 2001 at 03:46:26PM +0530, Gautam Pagedar wrote:
> Hello,
>
> i am new to python and need a basic answer. Hope someone would help me
> solve this.
>
> I want to execute a command ls -l and sore its result in a file.
>
> >>import sys
> >>import os
> >>os.system("ls -al")
>
> executes the command how can i redirect the to a file like we do in
> shell script using a redirection operator.
>
This works:
os.system("ls -al > foo")
Perhaps more usefully, though, the "commands" module allows you to get the
output of a program as a string:
foo = commands.getoutput("ls -al")
And it might be helpful to know that Python itself has functions for
listing files:
fileList = os.listdir(".")
As well as for getting information about them:
fileMTimes = [os.path.getmtime(f) for f in os.listdir(".")]
filePermissions = [os.stat(f).st_mode for f in os.listdir(".")]
For more information, see http://www.python.org/doc/lib/
Jp
More information about the Python-list
mailing list