Redirection problem

C

CSUIDL PROGRAMMEr

I am new to python. so be patient with me

I am trying to redirect the output of os.popen command to a file. I
want to append to that file. but instead of appending. The file only
shows last command that was writtenn to it.



filehandle= open("/root/yhpc-2.0/installer/yhpc-log" ,"a+");
filehandle.write(" Command executed is " + cmd);
try:
pipe= os.popen('%s > /root/yhpc-2.0/installer/yhpc-log' %cmd );
except : IOError;
filehandle.close();


Any suggestion would help.



filehandle.close();
 
G

Gabriel Genellina

En Mon, 23 Apr 2007 13:10:55 -0300, CSUIDL PROGRAMMEr
I am new to python. so be patient with me

I am trying to redirect the output of os.popen command to a file. I
want to append to that file. but instead of appending. The file only
shows last command that was writtenn to it.



filehandle= open("/root/yhpc-2.0/installer/yhpc-log" ,"a+");
filehandle.write(" Command executed is " + cmd);
try:
pipe= os.popen('%s > /root/yhpc-2.0/installer/yhpc-log' %cmd );
except : IOError;
filehandle.close();

There are two ways to redirect the output:
- using the shell, with > and >>
- using popen
You are mixing both here. Your command line uses >xxx-log, thus efectively
overwriting any previous content on the log file. You could do this:


logfile = open("....", "a+)
logfile.write("Command: %s\n" % cmd)
pipe = os.popen(cmd)
for line in pipe:
logfile.write(line)
pipe.close()
logfile.close()

Or try the subprocess module.
 
H

half.italian

I am new to python. so be patient with me

I am trying to redirect the output of os.popen command to a file. I
want to append to that file. but instead of appending. The file only
shows last command that was writtenn to it.

filehandle= open("/root/yhpc-2.0/installer/yhpc-log" ,"a+");
filehandle.write(" Command executed is " + cmd);
try:
pipe= os.popen('%s > /root/yhpc-2.0/installer/yhpc-log' %cmd );
except : IOError;
filehandle.close();

Any suggestion would help.

filehandle.close();

This works.

f = open("test", 'a')
stdout = os.popen("ls -l /")
f.write(stdout.read())
f.close()

~Sean
 

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
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top