Python loop through subprocess.check_output line by line

I need to iterate over the output of the command. I thought I was using subprocess.check_output , now I have two problems.

Here is the file:

 foo bar 

Here is my python script:

 import subprocess for line in subprocess.check_output(['cat', 'foo']): print "%r" % line 

And here is what I get:

 $ python subp.py 'f' 'o' 'o' '\n' 'b' 'a' 'r' '\n' 

I expect:

 $ python subp.py 'foo\n' 'bar\n' 
+7
source share
2 answers

subprocess.check_output (['cat', 'foo']) returns the string: "foo \ nbar"

Thus, the for loop repeats along the line, printing each character, one after the other.

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 
+13
source

With Python3, the previous answer doesn’t work right away, because bytes return check_output

Then you can either decode the bytes into a string, or immediately split them:

 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) 
0
source

Source: https://habr.com/ru/post/951764/


All Articles