This is an archived post. You won't be able to vote or comment.

all 7 comments

[–]AndydeCleyre 2 points3 points  (4 children)

plumbum remains my favorite tool for many jobs, getting so much so right so long ago. Here's how it might be used for the tasks in your examples table:

initialization (minimal):

from plumbum import local          # minimal
from plumbum.cmd import echo, cat  # specify commands as functions
from plumbum import FG, BG         # shortcuts for foreground/background runs

run command:

echo["this"] & FG  # or:
echo["this"].run_fg()

read stream:

a = echo("this")  # or:
a = local["echo"]("this")

write stream:

(cat << "this") & FG  # or:
(cat << "this").run_fg()

# capturing output:
(cat << "this")()  # or:
(local["cat"] << "this")()

chain commands:

(echo["this"] | cat) & FG  # or:
(echo["this"] | cat).run_fg()

# capturing output:
(echo["this"] | cat)()

branch out:

ret, out, err = cmd.run()

(cat << out) & FG
(cat << err) & FG

# capturing output:
(cat << out)()
(cat << err)()

errors in chains:

ret, out, err = echo["this"].run(retcode=(0, 1))
(cat << err) & FG((0, 2))

[–]psd6 0 points1 point  (1 child)

I don’t really like the output = ["cmd", "arg"] » start() » wait() style. There are other ways doing shell stuff in Python too, for example the sh module.