0

I am running in a loop an astronomical script written by another person. This is a minimal version of my loop:

from astro_script_file import some_astro_class

for file_name in ['file_1.txt', 'file_2.txt', 'file_n.txt']:
    some_astro_class.function_1(file_name)

When executing the above function_1 makes calculations based on the input txt-file and prints an output to the screen of the kind number of populated galaxies: some-number. The number of galaxies is declining with each iteration of my loop and I want to break the loop when the number reaches 0. For that I need to somehow read in the print statement or load it into a variable. I don't want to alter the astronomical script itself that I am provided with, instead I want to tinker only outside of it when I am executing the script.

Is this possible? Is there some way to load print statements when they get executed?

General Grievance
  • 4,555
  • 31
  • 31
  • 45
NeStack
  • 1,739
  • 1
  • 20
  • 40
  • Shouldn't `function_1` handle this case? I don't have more info but I imagine it will run forever without it. – Guy Jun 06 '23 at 11:19
  • Isn't it possible at all to alter `function_1`? reading the output seems like overkill compare to simply returning the number, and I don't know how you are going to save the number in a variable without editing the script. – Guy Jun 06 '23 at 11:24
  • @Guy Thanks! No, `function_1` doesn't handle the case of galaxy counts, it just spits them out. It's because `function_1` was not intended by the author to be be used in a loop. Even I most of the time don't use it in a loop. But for a special task now I have to. And the loop doesn't run forever, it runs until all files that I provide are covered – NeStack Jun 06 '23 at 11:27
  • To read the output you can probably do something like https://stackoverflow.com/a/40984270/5168011 – Guy Jun 06 '23 at 11:30

1 Answers1

-1
while true:
example = 5:
if example = 0:
print("Done")
else:
example =- 1

But i guess it would be different with files.