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?

4
  • Shouldn't function_1 handle this case? I don't have more info but I imagine it will run forever without it. Commented Jun 6, 2023 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. Commented Jun 6, 2023 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 Commented Jun 6, 2023 at 11:27
  • To read the output you can probably do something like stackoverflow.com/a/40984270/5168011 Commented Jun 6, 2023 at 11:30

1 Answer 1

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

But i guess it would be different with files.

Sign up to request clarification or add additional context in comments.

3 Comments

What this got to do with the question?
And pay attention to code formatting in your answers.
Aside from being irrelevant, it's also broken and will not even compile if the indentation is fixed... Please avoid submitting broken code, especially when it's extremely simple to test.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.