subprocess.Popen()/call() and appending file

H

hiral

Hi,

Do we have any facility to append file from Popen()/call(); see below
example...

1 import subprocess
2 f=open('log', 'w')
3 ...# writing some log-into into log file
4 p = subprocess.Popen(cmd, stdout=f, stderr=f) # (Q)
5 ...# do remaining stuff

Q: At line# 4, the output of the 'cmd' will wipe-out everything in
'log' file. So to avoid this do we have any mechanism to append into
the existing file from subprocess.

Currently I am doing following...
p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
o, e = p.communicate()
print >> log, o
print >> log, e

Pleaese let me know if there is any better mechanism.

Thank you in advance.
 
K

Kushal Kumaran

Hi,

Do we have any facility to append file from Popen()/call(); see below
example...

1 import subprocess
2 f=open('log', 'w')
3 ...# writing some log-into into log file
4 p = subprocess.Popen(cmd, stdout=f, stderr=f) # (Q)
5 ...# do remaining stuff

Q: At line# 4, the output of the 'cmd' will wipe-out everything in
'log' file. So to avoid this do we have any mechanism to append into
the existing file from subprocess.

Currently I am doing following...
p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
o, e = p.communicate()
print >> log, o
print >> log, e

Pleaese let me know if there is any better mechanism.

It is not actually wiping out the old contents. You might be running
into buffering issues. Explicitly flushing should help when switching
between the high level IO functions provided by file objects and the
low level IO that subprocess uses. Do a f.flush() before your
subprocess.Popen call.
 
H

hiral

It is not actually wiping out the old contents.  You might be running
into buffering issues.  Explicitly flushing should help when switching
between the high level IO functions provided by file objects and the
low level IO that subprocess uses.  Do a f.flush() before your
subprocess.Popen call.

--
regards,
kushal- Hide quoted text -

- Show quoted text -

Thanx it works fine.
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top