Run Python Script from another script and redirect script output to text file

Question:

I would like to run a second Python script and to redirect the output of this second script to a text file. The scripts and the text file are in the same folder.
I tried:

import sys
import os
    
path = 'my_path'  # working directory
os.chdir(path)
print("Current working directory: {0}".format(os.getcwd()))
        
sys.stdout = open("output.txt", "w")
execfile("script_I_want_to_run.py")
sys.stdout.close()

The program runs through once completely, after that the following error messages are shown

[SpyderKernelApp] ERROR | Exception in message handler:
Traceback (most recent call last):
  File "C:ProgramDataAnaconda3libsite-packagesipykernelkernelbase.py", line 367, in dispatch_shell
    await result
  File "C:ProgramDataAnaconda3libsite-packagesipykernelkernelbase.py", line 665, in execute_request
    sys.stdout.flush()
ValueError: I/O operation on closed file.
[SpyderKernelApp] ERROR | Error in message handler
Traceback (most recent call last):
  File "C:ProgramDataAnaconda3libsite-packagesipykernelkernelbase.py", line 471, in dispatch_queue
    await self.process_one()
  File "C:ProgramDataAnaconda3libsite-packagesipykernelkernelbase.py", line 460, in process_one
    await dispatch(*args)
  File "C:ProgramDataAnaconda3libsite-packagesipykernelkernelbase.py", line 379, in dispatch_shell
    sys.stdout.flush()
ValueError: I/O operation on closed file.

However, my script is still running.
In the txt file there is only the current working directory displayed.
At which point do I have to start?
Any Help will be appreciated!

Asked By: sbrc

||

Answers:

You can use the subprocess module. Using this submodule also makes the current working directory irrelevant as long as the path to your script is correct.

import subprocess

with open("output.txt", "wb") as output:
    with subprocess.Popen(args=["python", "path/to/script_I_want_to_run.py"], 
                          stdout=subprocess.PIPE) as script:
        output.write(script.stdout.read())
Answered By: Alexander

If you want to run a script from another script, then the process is quite simple. Especially if the second script is in the same folder as the first.

Just import the second script in exactly the same way as you do other imports.

So in the case of the question, this would be sufficient:

import script_I_want_to_run
Answered By: D.L
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.