'Unable to capture new lines from a console and save them into a text file

The following source code is supposed to capture output from the console and save that into a text file.

import sys
import subprocess
sys.stdout = open("my_app.txt", "w")
print(subprocess.check_output(
        ['./my_app', 
        '-length=7', 
        '-all', 
        '-file_names=tmpl.txt']        
        ), 
      '\n'
    )
sys.stdout.close()

The above source code is expected to output the following output:

1a62  A    4 THR H CCHHHHH 7.042 5.781 5.399 5.373 5.423  -9.118     
1a62  A    5 GLU H CHHHHHC 5.781 5.399 5.373 5.423 5.247   5.488   
1a62  A    6 LEU H HHHHHCC 5.399 5.373 5.423 5.247 5.485   5.166  

However, I am getting the following output:

1a62  A    4 THR H CCHHHHH 7.042 5.781 5.399 5.373 5.423  -9.118\n1a62  A    5 GLU H CHHHHHC 5.781 5.399 5.373 5.423 5.247   5.488\n1a62  A    6 LEU H HHHHHCC 5.399 5.373 5.423 5.247 5.485   5.166  

In other words, the new lines on stdout are not being recognized/captured/parsed properly.

How can I resolve this issue?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source