stdout in subprocess

Not To Miss not.to.miss at gmail.com
Wed Oct 27 16:24:29 EDT 2010


Hi all,

I have a question on subprocess.Popen, as shown comparably below. When I ran
1.py, it prints "0". I don't understand how the file is empty. Isn't it
flushed when the file object "out" is closed? On the other hand, running
2.py prints correct value.

####### 1.py ##########
from subprocess import *
out = open('foo.t', 'w')
a = Popen('ls -l', shell = True, stdout = out)
out.close()
print len(open('foo.t').readlines())


##### 2.py #########
from subprocess import *
out = open('foo.t', 'w')
a = Popen('ls -l', shell = True, stdout = PIPE, stderr = PIPE)
print >> out, a.communicate()[0]
out.close()
print len(open('foo.t').readlines())


Thanks
-- 
Best,
Zhenjiang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20101027/54af8129/attachment.html>


More information about the Python-list mailing list