Python Forum
printing out the contents aftre subprocess.call()
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
printing out the contents aftre subprocess.call()
#4
Maybe the java-program does not write to stdout.
Print also the stderr from the process.

If you put encoding="utf8" in the function call subprocess.run, you don't need to decode the raw bytes, then the function does it for you.
Use functions, to avoid code repetition.


Example:
#!/usr/bin/env python3
"""
Source for test.jar:
http://www.java2s.com/Code/Jar/t/Downloadtestjar.htm
"""

import subprocess


def get_int(question, allowed_values):
    while True:

        print("Please choose from", allowed_values)
        answer = input(question + ": ")

        try:
            value = int(answer)
        except ValueError:
            print("Input is not an integer")
            continue

        if value not in allowed_values:
            print(value, "is not in allowed_values")
            continue

        return value


def run_simple_client(info=False, cwd=None):
    cmd = ["java", "-jar", "test.jar"]

    if info:
        cmd.append("-info")

    return subprocess.run(cmd, capture_output=True, encoding="utf8", cwd=cwd)


def main():
    choices = (1, 2)
    choice = get_int("Please enter your choice", choices)
    print()

    if choice == 1:
        print("Without info")
        proc = run_simple_client()

    # could be an else-block, but maybe you want to extend the menu ...
    elif choice == 2:
        print("With info")
        proc = run_simple_client(info=True)

    print("stdout: ", proc.stdout.rstrip())
    print("stderr: ", proc.stderr.rstrip())
    print("returncode: ", proc.returncode)


if __name__ == "__main__":
    main()
Output:
[andre@andre-Fujitsu-i5 ~]$ LANG=C ./jj.py Please choose from (1, 2) Please enter your choice: 1 Without info stdout: stderr: no main manifest attribute, in test.jar returncode: 1
Docs:
Gribouillis and Rakshan like this post
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
RE: printing out the contents aftre subprocess.call() - by DeaD_EyE - Jul-30-2021, 08:27 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  continue if 'subprocess.call' failes tester_V 11 9,488 Aug-26-2021, 12:16 AM
Last Post: tester_V
  printing contents of Jar on timeout Rakshan 1 2,292 Jul-30-2021, 07:48 AM
Last Post: buran
  subprocess call cannot find the file specified RRR 6 21,389 Oct-15-2020, 11:29 AM
Last Post: RRR
  Why wont subprocess call work? steve_shambles 3 3,706 Apr-28-2020, 03:06 PM
Last Post: steve_shambles
  printing a list contents without brackets in a print statement paracelx 1 2,902 Feb-15-2020, 02:15 AM
Last Post: Larz60+
  subprocess.call - Help ! sniper6 0 1,932 Nov-27-2019, 07:42 PM
Last Post: sniper6
  Mock call to subprocess.Popen failing with StopIteration tharpa 7 7,670 Nov-08-2019, 05:00 PM
Last Post: Gribouillis
  Echo call to VLC bash using subprocess.Popen on Linux LuukS001 17 13,478 Jan-07-2019, 03:58 AM
Last Post: LuukS001
  Why is subprocess.call command not working? zBernie 5 12,477 Nov-19-2018, 11:11 PM
Last Post: snippsat
  Help with subprocess..call oldcity 1 3,108 Sep-28-2018, 03:59 PM
Last Post: volcano63

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020