Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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())
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading