I need to loop over the output of a command. I thought I'd use subprocess.check_output
, now I have two problems.
Here's a file:
foo
bar
Here's my python script:
import subprocess
for line in subprocess.check_output(['cat', 'foo']):
print "%r" % line
And here's what I get:
$ python subp.py
'f'
'o'
'o'
'\n'
'b'
'a'
'r'
'\n'
I expect:
$ python subp.py
'foo\n'
'bar\n'
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With