python loop over subprocess.check_output by line
With Python3 previous answer doesn't work immediately, because bytes
are returned by check_output
Then you can either decode bytes into a string or split them immediately:
output = subprocess.check_output(['cat', 'foo'])
# splitting with byte-string
for line in output.split(b'\n'):
print(line)
# or decoding output to usual string
output_str = output.decode()
for line in output_str.split('\n'):
print(line)
subprocess.check_output(['cat', 'foo']) returns a string: "foo\nbar"
Thus, your for loop iterates over the string, printing every character, one-by-one.
The following should fix your problem:
import subprocess
print subprocess.check_output(['cat', 'foo'])
You can also do:
import subprocess
for line in subprocess.check_output(['cat', 'foo']).split('\n'):
print "%r" % line