我必须将子进程的输出转储到以附加模式打开的文件中
from subprocess import Popen
fh1 = open("abc.txt", "a+") # this should have worked as per my understanding
# fh1.readlines() # Adding this solves the problem
p = Popen(["dir", "/b"], stdout = fh1, shell=True)
print p.communicate()[0]
fh1.close()
但是,上面的代码会覆盖我的文件abc.txt
不想要的文件,取消注释fh1.readlines()
会将光标移动到适当的位置,这是一个临时解决方案
有什么基本的我失踪了。
In [18]: fh1 = open("abc.txt",'a')
In [19]: fh1.tell() # This should be at the end of the file
Out[19]: 0L
In [20]: fh1 = open("abc.txt",'r')
In [21]: print fh1.readlines()
['1\n', '2\n', '3\n', '4\n', '5\n']