Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python loop over subprocess.check_output by line

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'
like image 803
bahamat Avatar asked Aug 14 '13 23:08

bahamat


2 Answers

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)

like image 134
Aleksandr Tukallo Avatar answered Oct 02 '22 06:10

Aleksandr Tukallo


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
like image 25
nicolas.leblanc Avatar answered Oct 02 '22 06:10

nicolas.leblanc