如果stderr字符串未以'\ n'

时间:2017-12-28 12:47:45

标签: python popen readline stderr raw-input

我有以下脚本b.py

import sys
sym = "NEW WORD"
print >> sys.stderr, "You must enter a single word.\n"
sys.stderr.write(("Please enter the correct word for %s\n"
                  "Or enter 'n' if it is not correct: ") % sym)
sym = raw_input()         
sys.stderr.write(("Please enter the correct word for %s\n"
                  "Or enter 'n' if it is not correct: ") % sym)

我正在尝试使用Popen运行此脚本,我正面临着这个问题。也就是说,在阅读了几行后,它就会卡住。如果我在correct:之后附加了'\ n',那么同样的工作(没有挂起)。

cmd2 = ['python', 'b.py']
In [43]: p =  Popen(cmd2 , stdin=PIPE, stdout=PIPE, stderr=PIPE, universal_newlines=True)
In [44]: p.stderr.readline()
Out[44]: 'You must enter a single word.\n'
In [45]: p.stderr.readline()
Out[45]: '\n'
In [46]: p.stderr.readline()
Out[46]: 'Please enter the correct word for NEW WORD\n'
In [47]: p.stderr.readline()
<Hangs>

为什么会这样?也有任何解决方案(除了对b.py进行任何更改)

但是当我手动运行脚本b.py

时,情况正常
python b.py
You must enter a single word.

Please enter the correct word for NEW WORD
Or enter 'n' if it is not correct: HELLO
Please enter the correct word for HELLO
Or enter 'n' if it is not correct: 

0 个答案:

没有答案