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

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

    """
    Ex: Dialog (2-way) with a Popen()
    """
    
    p = subprocess.Popen('Your Command Here',
                     stdout=subprocess.PIPE,
                     stderr=subprocess.STDOUT,
                     stdin=PIPE,
                     shell=True,
                     bufsize=0)
    p.stdin.write('START\n')
    out = p.stdout.readline()
    while out:
      line = out
      line = line.rstrip("\n")
    
      if "WHATEVER1" in line:
          pr = 1
          p.stdin.write('DO 1\n')
          out = p.stdout.readline()
          continue
    
      if "WHATEVER2" in line:
          pr = 2
          p.stdin.write('DO 2\n')
          out = p.stdout.readline()
          continue
    """
    ..........
    """
    
    out = p.stdout.readline()
    
    p.wait()
    

提交回复
热议问题