In this code, subprocess.Popen creates /tmp/test.txt with date output.
Why doesn't the 2nd "print lines" work ?
I would appreciate for your help.
test]# touch /tmp/test.txt
test]# ./x1.py
/tmp/test.txt
()
/tmp/test.txt
()
test]# ./x1.py
/tmp/test.txt
('Sun Jun 19 15:10:21 PDT 2016\n',)
/tmp/test.txt
()
test]# cat x1.py
#!/usr/bin/python
from subprocess import Popen, PIPE
filename = "/tmp/test.txt"
lines = tuple(open(filename, 'r'))
print filename
print lines # this is not empty
file_ = open(filename, "w")
Popen("date", shell=True, stdout=file_)
file_.close()
lines = tuple(open(filename, 'r'))
print filename
# why is this empty even if file_ definitely created the /tmp/test.txt ?
print lines
test]#
.wait()ing for the Popen to finish before closing itsstdout?