I have the following line in a Python script that runs a separate Python script from within the original script:
subprocess.Popen("'/MyExternalPythonScript.py' " + theArgumentToPassToPythonScript, shell=True).communicate()
Using the above line, any print()
statements found in the separate Python file do appear in the console of the main Python script.
However, these statements are not reflected in the .txt file log that the script writes to.
Does anyone know how to fix this, so that the .txt file exactly reflects the true console text of the main Python script?
This is the method I am using to save the console as a .txt file, in real time:
import sys
class Logger(object):
def __init__(self):
self.terminal = sys.stdout
self.log = open("/ScriptLog.txt", "w", 0)
def write(self, message):
self.terminal.write(message)
self.log.write(message)
sys.stdout = Logger()
I am not necessarily attached to this method. I am interested in any method that will achieve what I've detailed.