0

I have two Python scripts. The first, when run on a text file, outputs ten lines of text to the console (I cannot paste the code here, unfortunately).

The second Python script successfully runs the first, and runs the script in a loop numerous times - again, outputing to the console.

I cannot, however, work out how to capture this output into a variable in my second script (so I can save it to a file). Attempting to capture this output into a variable (just to try printing) just gives many lines of error. (I'm sure this is a newbie error).

import sh
import 1st-script

n = 10

for i in range(n):
   list = exec(open("1st-script.py").read())
   print(list)

I understand that this is something to do with the exec() not making its output available for inclusion in the variable. So any thoughts would be great.

  • Alternatively: https://stackoverflow.com/questions/3906232/python-get-the-print-output-in-an-exec-statement – blurry Mar 11 '22 at 16:21
  • If you are just trying to print to a file instead of to the console now, you can use the `file=` kwarg on the `print` function - i.e. `print(.., file=file_object)` – Mortz Mar 11 '22 at 16:24

0 Answers0