getting from command line options to file

Michael Hoffman cam.ac.uk at mh391.invalid
Mon Apr 16 15:08:24 EDT 2007


CSUIDL PROGRAMMEr wrote:
> hi folks
> I am new to python. I have a module does call a os.command(cmd) where
> cmd is a rpm command.
> Instead of using os.command and getting the results on command line ,
> i would like to dump the output in a file. Is os.command(cmd >
> filename)  the most efficient command??

I think the best thing to do would be something like this (Python 2.5):

from __future__ import with_statement
import subprocess

with file("test.out", "w") as outfile:
     subprocess.check_call(["ls", "/etc"], stdout=outfile)
-- 
Michael Hoffman



More information about the Python-list mailing list