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

后端 未结 11 2775
半阙折子戏
半阙折子戏 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:05

    I am using python3 and found out that you need to encode your string before you can pass it into stdin:

    p = Popen(['grep', 'f'], stdout=PIPE, stdin=PIPE, stderr=PIPE)
    out, err = p.communicate(input='one\ntwo\nthree\nfour\nfive\nsix\n'.encode())
    print(out)
    

提交回复
热议问题