Use text=true with subprocess functions to get stdout/stderr as str, not bytes
I often use the subprocess module to capture the output of external processes in Python, for example:
>>> import subprocess
>>> subprocess.check_output(["echo", "hello world"])
b'hello world\n'
This returns the output as bytes, and so I usually have to decode the output to get a str. (And I can never remember whether I need to encode/decode when converting bytes to str, so I get it wrong half the time.)
I recently discovered I can pass text=true to subprocess functions, and it does the work of decoding the output for me. For example:
>>> subprocess.check_output(["echo", "hello world"], text=True)
'hello world\n'
It’s a small thing, but it’ll make all my scripts that use subprocess a bit easier to follow.
(I discovered this option when I learnt that you can iterate over subprocess output line-by-line.)