This is an extension of the mantazer answer in python3. You can still use subprocess.check_output command in python3:
>>> subprocess.check_output(["echo", "hello world"]) b'hello world\n'
however now it gives us a byte string. To get the real python string, we need to use decoding:
>>> subprocess.check_output(["echo", "hello world"]).decode(sys.stdout.encoding) 'hello world\n'
Using sys.stdout.encoding as the encoding, and not just the default UTF-8 should do this job for any OS (at least theoretically).
The final new line (and any other extra spaces) can be easily removed with .strip() , so the last command:
>>> subprocess.check_output(["echo", "hello world"] ).decode(sys.stdout.encoding).strip() 'hello world'
dshepherd
source share