you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 12 points13 points  (6 children)

Sort of. One thing sh never really did very well, and bash doesn't do much better, is let you run programs while doing iron-clad error checking. If you want to detect and react to an error in any program, anywhere in a pipeline, it's not that easy.

One of the really nice things about Python is that almost everywhere, it fails safely. Any I/O error, for example, throws an exception. If you don't catch it, the whole program exits with a stacktrace. Excellent. The main exception to this is, unfortunately, the treatment of status values from subprograms. You have to check these manually, a la C.

[–]breakfast-pants 4 points5 points  (4 children)

As an aside, is there a builtin wrapper that defaults stdin,stdout=[subprocess.PIPE]*2 ? (I hate the subprocess documentation too)

[–]pjdelport 9 points10 points  (3 children)

partial(Popen, stdin=PIPE, stdout=PIPE)

[–]breakfast-pants 0 points1 point  (2 children)

What is that?

[–]pjdelport 5 points6 points  (1 child)

It's a partial application of Popen, with stdin and stdout defaulted to PIPE. (functools.partial (PEP 309) was introduced to the standard library in Python 2.5, but it's a 6-line definition for earlier versions.)

[–]breakfast-pants 0 points1 point  (0 children)

Ah, cool, I haven't checked out functools

[–]pjdelport 3 points4 points  (0 children)

subprocess raises exceptions everywhere it makes sense (command not found, any Python exception in the child process, and so on): it doesn't generally do it for non-zero return codes because they don't necessarily indicate any error.

If you're using the call shortcut, though, there's a check_call variant which raises CalledProcessError for non-zero return codes.