Execute complex shell commands within python and obtain the output.

Hongyi Zhao hongyi.zhao at gmail.com
Mon Sep 2 23:59:06 EDT 2019


On Tue, 03 Sep 2019 08:24:17 +1000, Cameron Simpson wrote:

> It seems reasonable to me, but I would not use stderr=subprocess.STDOUT.
> Why did you do that? The stderr stream pretty much exists to avoid
> polluting stdout with error messages.

Thanks for your suggestion.

> 
> For really complex stuff you're often better writing a shell script and
> invoking that script, and avoiding shell=True. This has the advantages
> that:
> 
> (a) it avoids shell=True, a common source of accidents
> 
> (b) you don't have to embed shell punctuation in a Python string, which
> itself has punctuation
> 
> (c) you can run the script yourself by hand for testing purposes,
> outside the Python programme
> 
> (d) you're not restricted to the shell; the script might be in awk or
> any number of other languages
> 
> Finally, the .decode('utf8') assumes your locale is UTF8 based. It
> probably is, but if it isn't then you may get mojibake.

Nowadays, most of the os use utf8 as the default locale.  Am I wrong?





More information about the Python-list mailing list