I have a Python script which executes a system call (the call is actually a java program).
I am using Popen.communicate() to attempt to capture the stdout and stderr. However, it only works for some Java applications (of which, ALL the java applications I'm calling do display something to the screen when executed directly in the shell).
The code I am using is as follows:
    # Perform the java call
    try:
        p = subprocess.Popen(args,
                             stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE)
    except OSError as e:
        error_msg = '  Error reading file while executing system command: {0}'.format(e.message)
    except ValueError as e:
        error_msg = '  Error in arugments while executing system command: {0}'.format(e.message)
    else:
        # Display the results in real time
        for line in iter(p.stdout.readline, ''):
            sys.stdout.write(line)
        # Grab the stdout and stderr from the process.
        output, err = p.communicate()
    finally:
        # Check to see if an internal error occurred. If not, flip the flag
        if not err and not error_msg:
            java_completed = True
        else:
            java_completed = False
In some cases, the line sys.stdout.write(line) is never called, and in other's it is.
Does anyone have any experience with this?