Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6868997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:30:13+00:00 2026-05-27T03:30:13+00:00

I am trying to get output from a subprocess and then give commands to

  • 0

I am trying to get output from a subprocess and then give commands to that process based on the preceding output. I need to do this a variable number of times, when the program needs further input. (I also need to be able to hide the subprocess command prompt if possible).

I figured this would be an easy task given that I have seen this problem being discussed in posts from 2003 and it is nearly 2012 and it appears to be a pretty common need and really seems like it should be a basic part of any programming language. Apparently I was wrong and somehow almost 9 years later there is still no standard way of accomplishing this task in a stable, non-destructive, platform independent way!

I don’t really understand much about file i/o and buffering or threading so I would prefer a solution that is as simple as possible. If there is a module that accomplishes this that is compatible with python 3.x, I would be very willing to download it. I realize that there are multiple questions that ask basically the same thing, but I have yet to find an answer that addresses the simple task that I am trying to accomplish.

Here is the code I have so far based on a variety of sources; however I have absolutely no idea what to do next. All my attempts ended in failure and some managed to use 100% of my CPU (to do basically nothing) and would not quit.

import subprocess
from subprocess import Popen, PIPE
p = Popen(r'C:\postgis_testing\shellcomm.bat',stdin=PIPE,stdout=PIPE,stderr=subprocess.STDOUT shell=True)
stdout,stdin = p.communicate(b'command string')

In case my question is unclear I am posting the text of the sample batch file that I demonstrates a situation in which it is necessary to send multiple commands to the subprocess (if you type an incorrect command string the program loops).

@echo off
:looper
set INPUT=
set /P INPUT=Type the correct command string:
if "%INPUT%" == "command string" (echo you are correct) else (goto looper)

If anyone can help me I would very much appreciate it, and I’m sure many others would as well!

EDIT here is the functional code using eryksun’s code (next post) :

import subprocess
import threading
import time
import sys

try: 
    import queue
except ImportError:
    import Queue as queue

def read_stdout(stdout, q, p):
    it = iter(lambda: stdout.read(1), b'')
    for c in it:
        q.put(c)
        if stdout.closed:
            break

_encoding = getattr(sys.stdout, 'encoding', 'latin-1')
def get_stdout(q, encoding=_encoding):
    out = []
    while 1:
        try:
            out.append(q.get(timeout=0.2))
        except queue.Empty:
            break
    return b''.join(out).rstrip().decode(encoding)

def printout(q):
    outdata = get_stdout(q)
    if outdata:
        print('Output: %s' % outdata)

if __name__ == '__main__':
    #setup
    p = subprocess.Popen(['shellcomm.bat'], stdin=subprocess.PIPE, 
                     stdout=subprocess.PIPE, stderr=subprocess.PIPE, 
                     bufsize=0, shell=True) # I put shell=True to hide prompt
    q = queue.Queue()
    encoding = getattr(sys.stdin, 'encoding', 'utf-8')

    #for reading stdout
    t = threading.Thread(target=read_stdout, args=(p.stdout, q, p))
    t.daemon = True
    t.start()

    #command loop
    while p.poll() is None:
        printout(q)
        cmd = input('Input: ')
        cmd = (cmd + '\n').encode(encoding)
        p.stdin.write(cmd)
        time.sleep(0.1) # I added this to give some time to check for closure (otherwise it doesn't work)

    #tear down
    for n in range(4):
        rc = p.poll()
        if rc is not None:
            break
        time.sleep(0.25)
    else:
        p.terminate()
        rc = p.poll()
        if rc is None:
            rc = 1

    printout(q)
    print('Return Code: %d' % rc)

However when the script is run from a command prompt the following happens:

C:\Users\username>python C:\postgis_testing\shellcomm7.py
Input: sth
Traceback (most recent call last):
File "C:\postgis_testing\shellcomm7.py", line 51, in <module>
    p.stdin.write(cmd)
IOError: [Errno 22] Invalid argument

It seems that the program closes out when run from command prompt. any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T03:30:14+00:00Added an answer on May 27, 2026 at 3:30 am

    This demo uses a dedicated thread to read from stdout. If you search around, I’m sure you can find a more complete implementation written up in an object oriented interface. At least I can say this is working for me with your provided batch file in both Python 2.7.2 and 3.2.2.

    shellcomm.bat:

    @echo off
    echo Command Loop Test
    echo.
    :looper
    set INPUT=
    set /P INPUT=Type the correct command string:
    if "%INPUT%" == "command string" (echo you are correct) else (goto looper)
    

    Here’s what I get for output based on the sequence of commands “wrong”, “still wrong”, and “command string”:

    Output:
    Command Loop Test
    
    Type the correct command string:
    Input: wrong
    Output:
    Type the correct command string:
    Input: still wrong
    Output:
    Type the correct command string:
    Input: command string
    Output:
    you are correct
    
    Return Code: 0
    

    For reading the piped output, readline might work sometimes, but set /P INPUT in the batch file naturally isn’t writing a line ending. So instead I used lambda: stdout.read(1) to read a byte at a time (not so efficient, but it works). The reading function puts the data on a queue. The main thread gets the output from the queue after it writes a a command. Using a timeout on the get call here makes it wait a small amount of time to ensure the program is waiting for input. Instead you could check the output for prompts to know when the program is expecting input.

    All that said, you can’t expect a setup like this to work universally because the console program you’re trying to interact with might buffer its output when piped. In Unix systems there are some utility commands available that you can insert into a pipe to modify the buffering to be non-buffered, line-buffered, or a given size — such as stdbuf. There are also ways to trick the program into thinking it’s connected to a pty (see pexpect). However, I don’t know a way around this problem on Windows if you don’t have access to the program’s source code to explicitly set the buffering using setvbuf.

    import subprocess
    import threading
    import time
    import sys
    
    if sys.version_info.major >= 3:
        import queue
    else:
        import Queue as queue
        input = raw_input
    
    def read_stdout(stdout, q):
        it = iter(lambda: stdout.read(1), b'')
        for c in it:
            q.put(c)
            if stdout.closed:
                break
    
    _encoding = getattr(sys.stdout, 'encoding', 'latin-1')
    def get_stdout(q, encoding=_encoding):
        out = []
        while 1:
            try:
                out.append(q.get(timeout=0.2))
            except queue.Empty:
                break
        return b''.join(out).rstrip().decode(encoding)
    
    def printout(q):
        outdata = get_stdout(q)
        if outdata:
            print('Output:\n%s' % outdata)
    
    if __name__ == '__main__':
    
        ARGS = ["shellcomm.bat"]   ### Modify this
    
        #setup
        p = subprocess.Popen(ARGS, bufsize=0, stdin=subprocess.PIPE, 
                             stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        q = queue.Queue()
        encoding = getattr(sys.stdin, 'encoding', 'utf-8')
    
        #for reading stdout
        t = threading.Thread(target=read_stdout, args=(p.stdout, q))
        t.daemon = True
        t.start()
    
        #command loop
        while 1:
            printout(q)
            if p.poll() is not None or p.stdin.closed:
                break
            cmd = input('Input: ') 
            cmd = (cmd + '\n').encode(encoding)
            p.stdin.write(cmd)
    
        #tear down
        for n in range(4):
            rc = p.poll()
            if rc is not None:
                break
            time.sleep(0.25)
        else:
            p.terminate()
            rc = p.poll()
            if rc is None:
                rc = 1
    
        printout(q)
        print('\nReturn Code: %d' % rc)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get the output from a servlet on an Android phone. This
I'm trying to get console output from a program using this library uZpRunConsoleApp .
Possible Duplicate: Get output from a process Executing DOS commands from Java I am
I'm trying to get output from a python multiprocessing Process displayed in a Tkinter
I am trying to get SED to transform the output from a TMS320C55x compiler
I am currently trying to get a script to write output from other started
I'm trying to get the output of the query 'SHOW TABLES FROM database_name' into
I have some trouble with python. I am trying to get output from a
I am trying to get command output from Linux box eg: tnsping and I
Trying to get close a W3C validation of the output from my aspx (VB.NET

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.