Skip to main content

run a subprocess and capture its CONOUT$ console output

Project description

run a subprocess and capture its CONOUT$ console output

Tested against Windows / Python 3.11 / Anaconda

pip install subprocconout

subprocess_conout(*args, nrows=9999, encode=True, **kwargs):
    Function to run a subprocess and capture its CONOUT$ console output.

    Args:
        *args: Variable length argument list.
        nrows: Number of rows to capture from the console output (default is 9999).
        encode: Boolean indicating whether to encode the console output (default is True).
        **kwargs: Variable length keyword argument list.

    Returns:
        The captured console output.
		
from subprocconout import subprocess_conout
import textwrap
# based on https://stackoverflow.com/a/38749458/15096247
text = textwrap.dedent(
    """\
    Lorem ipsum dolor sit äçamet, consectetur adipiscing elit, sed do
    eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut
    enim ad minim veniam, quis nostrud exercitation ullamco laboris
    nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
    in reprehenderit in voluptate velit esse cillum dolore eu
    fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
    proident, sunt in culpa qui officia deserunt mollit anim id est
    laborum."""
)

cmd = (
    'python -c "'
    "print('piped output');"
    "conout = open(r'CONOUT$', 'w');"
    "conout.write('''%s''')\"" % text
)

result = subprocess_conout(cmd, nrows=9999, encode=True)
print([[q for q in line.split(b"\x00") if q] for line in result if line])

# [
#     [
#         b"Lorem ipsum dolor sit \x84\x87amet, consectetur adipiscing elit, sed do",
#         b"eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut",
#     ],
#     [
#         b"enim ad minim veniam, quis nostrud exercitation ullamco laboris",
#         b"nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor",
#     ],
#     [
#         b"in reprehenderit in voluptate velit esse cillum dolore eu",
#         b"fugiat nulla pariatur. Excepteur sint occaecat cupidatat non",
#     ],
#     [
#         b"proident, sunt in culpa qui officia deserunt mollit anim id est",
#         b"laborum.",
#     ],
# ]
		

Project details


Release history Release notifications | RSS feed

This version

0.10

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

subprocconout-0.10.tar.gz (5.4 kB view hashes)

Uploaded Source

Built Distribution

subprocconout-0.10-py3-none-any.whl (6.7 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page