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

后端 未结 11 2782
半阙折子戏
半阙折子戏 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 01:59

    I'm a bit surprised nobody suggested creating a pipe, which is in my opinion the far simplest way to pass a string to stdin of a subprocess:

    read, write = os.pipe()
    os.write(write, "stdin input here")
    os.close(write)
    
    subprocess.check_call(['your-command'], stdin=read)
    

提交回复
热议问题