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

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

    Beware that Popen.communicate(input=s)may give you trouble ifsis too big, because apparently the parent process will buffer it before forking the child subprocess, meaning it needs "twice as much" used memory at that point (at least according to the "under the hood" explanation and linked documentation found here). In my particular case,swas a generator that was first fully expanded and only then written tostdin so the parent process was huge right before the child was spawned, and no memory was left to fork it:

    File "/opt/local/stow/python-2.7.2/lib/python2.7/subprocess.py", line 1130, in _execute_child self.pid = os.fork() OSError: [Errno 12] Cannot allocate memory

    0 讨论(0)
  • 2020-11-22 01:55

    Popen.communicate() documentation:

    Note that if you want to send data to the process’s stdin, you need to create the Popen object with stdin=PIPE. Similarly, to get anything other than None in the result tuple, you need to give stdout=PIPE and/or stderr=PIPE too.

    Replacing os.popen*

        pipe = os.popen(cmd, 'w', bufsize)
        # ==>
        pipe = Popen(cmd, shell=True, bufsize=bufsize, stdin=PIPE).stdin
    

    Warning Use communicate() rather than stdin.write(), stdout.read() or stderr.read() to avoid deadlocks due to any of the other OS pipe buffers filling up and blocking the child process.

    So your example could be written as follows:

    from subprocess import Popen, PIPE, STDOUT
    
    p = Popen(['grep', 'f'], stdout=PIPE, stdin=PIPE, stderr=STDOUT)    
    grep_stdout = p.communicate(input=b'one\ntwo\nthree\nfour\nfive\nsix\n')[0]
    print(grep_stdout.decode())
    # -> four
    # -> five
    # ->
    

    On Python 3.5+ (3.6+ for encoding), you could use subprocess.run, to pass input as a string to an external command and get its exit status, and its output as a string back in one call:

    #!/usr/bin/env python3
    from subprocess import run, PIPE
    
    p = run(['grep', 'f'], stdout=PIPE,
            input='one\ntwo\nthree\nfour\nfive\nsix\n', encoding='ascii')
    print(p.returncode)
    # -> 0
    print(p.stdout)
    # -> four
    # -> five
    # -> 
    
    0 讨论(0)
  • 2020-11-22 01:55

    On Python 3.7+ do this:

    my_data = "whatever you want\nshould match this f"
    subprocess.run(["grep", "f"], text=True, input=my_data)
    

    and you'll probably want to add capture_output=True to get the output of running the command as a string.

    On older versions of Python, replace text=True with universal_newlines=True:

    subprocess.run(["grep", "f"], universal_newlines=True, input=my_data)
    
    0 讨论(0)
  • 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()
    
    0 讨论(0)
  • 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)
    
    0 讨论(0)
  • 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)
    
    0 讨论(0)
提交回复
热议问题