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

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

    from subprocess import Popen, PIPE
    from tempfile import SpooledTemporaryFile as tempfile
    f = tempfile()
    f.write('one\ntwo\nthree\nfour\nfive\nsix\n')
    f.seek(0)
    print Popen(['/bin/grep','f'],stdout=PIPE,stdin=f).stdout.read()
    f.close()
    

提交回复
热议问题