getting from command line options to file

C

CSUIDL PROGRAMMEr

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??


thanks
 
M

Michael Hoffman

CSUIDL said:
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)
 
C

CSUIDL PROGRAMMEr

CSUIDL said:
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)


but what if i have python which is 2.4??
 
G

Gabriel Genellina

En Fri, 20 Apr 2007 17:48:10 -0300, CSUIDL PROGRAMMEr
CSUIDL said:
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)


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()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top