次の例があります。
import subprocess
p = subprocess.Popen("cmd",stdin = subprocess.PIPE, stdout=subprocess.PIPE )
p.stdin.write(b'cd\\' + b'\r\n')
p.stdin.write(b'dir' + b'\r\n')
p.stdin.write(b'\r\n')
while True:
line = p.stdout.readline()
print(line.decode('ascii'), end='')
if line.rstrip().decode('ascii') == 'C:\>': #I need to check at this point if there is new data in the PIPE
print('End of File')
break
サブプロセスからの出力を PIPE でリッスンしていますが、PIPE から新しいデータが来ない場合は、読み取りを停止したいと思います。PIPE が空であることを示す制御ステートメントが必要です。これは、プロセスがフリーズしたり、予期しない結果で終了した場合の問題を回避するのに役立ちます。