PYTHON VERSION - 3.5.2 OS - Ubuntu 16.04 LTS
I am currently using both stdout and print statements to write to the terminal. I want to capture the output of ONLY sys.stdout.write command and not the print commands So for eg if my code is -
import sys sys.stdout.write("Hello") print("PRINT") I want to capture only "Hello" and not "PRINT".
I am currently using this :
x = subprocess.run(['python3 test.py'] , shell = True , stdout = subprocess.PIPE).stdout.decode('utf-8') which gives me this output:
['HelloPRINT', '']
print()usessys.stdout.write()to display text. But suproccess see only text send by system and it doesn't know what you used to send text to system.printwrites tosys.stdoutby default. You can, however, override this behavior in thefileargument of theprintfunction.print()to file orstderrbut you have to do it in codeprint("Print", file=sys.stderr)sys.stderrwhat happens to the text ?stderr- but subprocess will get onlystdoutbecause you set onlystdout = subprocess.PIPE