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

1k Views Asked by At

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:\ProgramData\Anaconda3\lib\site-packages\ipykernel\kernelbase.py", line 367, in dispatch_shell
    await result
  File "C:\ProgramData\Anaconda3\lib\site-packages\ipykernel\kernelbase.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:\ProgramData\Anaconda3\lib\site-packages\ipykernel\kernelbase.py", line 471, in dispatch_queue
    await self.process_one()
  File "C:\ProgramData\Anaconda3\lib\site-packages\ipykernel\kernelbase.py", line 460, in process_one
    await dispatch(*args)
  File "C:\ProgramData\Anaconda3\lib\site-packages\ipykernel\kernelbase.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!

2

There are 2 best solutions below

8
Alexander On BEST ANSWER

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())
0
D.L On

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