How do I pass a string into subprocess.Popen (using the stdin argument)?

后端 未结 11 2778
半阙折子戏
半阙折子戏 2020-11-22 01:33

If I do the following:

import subprocess
from cStringIO import StringIO
subprocess.Popen([\'grep\',\'f\'],stdout=subprocess.PIPE,stdin=StringIO(\'one\\ntwo\\         


        
11条回答
  •  北恋
    北恋 (楼主)
    2020-11-22 02:02

    p = Popen(['grep', 'f'], stdout=PIPE, stdin=PIPE, stderr=STDOUT)    
    p.stdin.write('one\n')
    time.sleep(0.5)
    p.stdin.write('two\n')
    time.sleep(0.5)
    p.stdin.write('three\n')
    time.sleep(0.5)
    testresult = p.communicate()[0]
    time.sleep(0.5)
    print(testresult)
    

提交回复
热议问题