getting from command line options to file

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Fri Apr 20 17:10:59 EDT 2007


En Fri, 20 Apr 2007 17:48:10 -0300, CSUIDL PROGRAMMEr  
<syedamjad_a at yahoo.com> escribió:

> On Apr 16, 1:08 pm, Michael Hoffman <cam.ac... at mh391.invalid> wrote:
>> 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
>
>
> but what if i have python which  is 2.4??

Omit the with statement:

import subprocess

outfile = open("test.out", "w")
try:
   subprocess.check_call(["ls", "/etc"], stdout=outfile)
finally:
   outfile.close()

-- 
Gabriel Genellina



More information about the Python-list mailing list