Skip to main content

Run a command in a subprocess and yield lines of text from stdout and stderr

EXAMPLES

import subprocessor as sub

CMD = 'my-unix-command "My Cool File.txt" No-file.txt'

for ok, line in sub.Sub(CMD) as sp:
    if ok:
         print(' ', line)
    else:
         print('!', line)

if sp.returncode:
    print('Error code', sp.returncode)

# Return two lists of text lines and a returncode
out_lines, err_lines, returncode = sub.run(CMD)

# Call callback functions with lines of text read from stdout and stderr
returncode = sub.call(CMD, save_results, print_errors)

# Log stdout and stderr, with prefixes
returncode = sub.log(CMD)

API

Methods on class subprocessor.Sub:

Sub.__init__(self, cmd, **kwds)

Iterate over lines of text from a subprocess.

If kwargs['shell'] is true, Popen expects a string, and so if cmd is not a string, it is joined using shlex.

If kwargs['shell'] is false, Popen expects a list of strings, and so if cmd is a string, it is split using shlex.

ARGUMENTS
cmd:

The command to run in a subprocess: a string or a list or tuple of strings

kwargs:

Keyword arguments passed to subprocess.Popen()

Sub.__iter__(self)

Yields a sequence of ok, line pairs from stdout and stderr of a subprocess, where ok is True if line came from stdout and False if it came from stderr.

After iteration is done, the .returncode property contains the error code from the subprocess, an integer where 0 means no error.

Sub.call(self, out=None, err=None)

Run the subprocess and call function out with lines from stdout and function err with lines from stderr.

Blocks until the subprocess is complete: the callbacks are on the current thread.

ARGUMENTS
out:

if not None, out is called for each line from the subprocess’s stdout

err:

if not None, err is called for each line from the subprocess’s stderr,

Sub.run(self)

Reads lines from stdout and stderr into two lists out and err, then returns a tuple (out, err, returncode)

Sub.log(self, out=' ', err='! ', print=<built-in function print>)

Read lines from stdin and stderr and prints them with prefixes

Returns the shell integer error code from the subprocess, where 0 means no error.

ARGUMENTS
out:

Prefix for printing lines from stdout

err:

Prefix for printing lines from stderr

Functions

subprocessor.call(cmd, out=None, err=None, **kwds)

Run the subprocess and call function out with lines from stdout and function err with lines from stderr.

Blocks until the subprocess is complete: the callbacks are on the current thread.

ARGUMENTS
cmd:

The command to run in a subprocess: a string or a list or tuple of strings

out:

if not None, out is called for each line from the subprocess’s stdout

err:

if not None, err is called for each line from the subprocess’s stderr,

kwargs:

Keyword arguments passed to subprocess.Popen()

subprocessor.run(cmd, **kwds)

Reads lines from stdout and stderr into two lists out and err, then returns a tuple (out, err, returncode)

ARGUMENTS
cmd:

The command to run in a subprocess: a string or a list or tuple of strings

kwargs:

Keyword arguments passed to subprocess.Popen()

subprocessor.log(cmd, out=' ', err='! ', print=<built-in function print>, **kwds)

Read lines from stdin and stderr and prints them with prefixes

Returns the shell integer error code from the subprocess, where 0 means no error.

ARGUMENTS
cmd:

The command to run in a subprocess: a string or a list or tuple of strings

out:

Prefix for printing lines from stdout

err:

Prefix for printing lines from stderr

kwargs:

Keyword arguments passed to subprocess.Popen()

Release files for subprocessor 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for subprocessor 1.0.0
File Size Uploaded
subprocessor-1.0.0.tar.gz 4.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for subprocessor 1.0.0
File Interpreter ABI Platform
subprocessor-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 9.3 kB

Release files / subprocessor-1.0.0.tar.gz

Download URL subprocessor-1.0.0.tar.gz
Size 4.0 kB
Tags Source
SHA-256 checksum
How to use checksums
ce1c3b6b0b67a110ec3117e03f30f916d58eebeb62d5afee9ed3812395cd239b
BLAKE2b-256 checksum
How to use checksums
6270eed8b9653afeb573e435accd98e0da05ecfe7d174da8a45c6ae9c970eea1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.1.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.8.1

Release files / subprocessor-1.0.0-py3-none-any.whl

Download URL subprocessor-1.0.0-py3-none-any.whl
Size 5.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
63e72999662c9900faffc12b7da15b8398ffb20adb16055a350313bb64472913
BLAKE2b-256 checksum
How to use checksums
1ee578cad2672003931d3b302d6a0aa28f011ba44779f6986083d63ed3524fcd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.1.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.8.1

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

0.9.2

2 release files

0.9.1

2 release files

0.9.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page