I want to get the output of a python script which is made to run in cmd to a variable in the host script. I am partially able to do it but not getting the desired output. Please guys before downvoting and closing this question, note that this is different from other questions regarding the same issue
Here is the code that i wrote as my script that needs to be run:
import click #-->pip install click
import sys
import time
#number of iterations taken
iters = 5
#record the starting time
start = time.time()
#iterate through loop
for j in range(1, iters + 1):
print(f"iteration {j} out of {iters}.")
#Generate progressbar
with click.progressbar(range(1)) as bar:
for i in bar:
pass
#End of Progressbar
#Record the Ending time
end = time.time()
print(f"Execution took approximately {round((end-start), 3)} seconds") #print the time taken rounded off to 3 decimals
Here is the out that i need(i get the output when i run the script natively):
iteration 1 out of 5.
[####################################] 100%
iteration 2 out of 5.
[####################################] 100%
iteration 3 out of 5.
[####################################] 100%
iteration 4 out of 5.
[####################################] 100%
iteration 5 out of 5.
Note that i want the output to be stored in my variable every time a new character is printed on the screen. i.e in realtime.
here is the code that i wrote using subprocesses to get the output of the file:
import subprocess
from subprocess import run, PIPE
cmd = 'python iter.py'
output = subprocess.run(cmd, shell=True, universal_newlines=True, stdout=PIPE, stderr=PIPE) #variable that i want to store the value in.
print(output.stdout)
But unfortunately, it is outputting only this:
iteration 1 out of 5.
iteration 2 out of 5.
iteration 3 out of 5.
iteration 4 out of 5.
iteration 5 out of 5.
Execution took approximately 0.001 seconds
Clearly, i have lost my progressbar! and that is essential for what i'm working on! How do i make sure that even the progressbar is stored in the variable.
NOTE - I STRICTLY NEED THE OUTPUT TO BE STORED IN A VARIABLE SO THAT I CAN REFERENCE IT IN THE FUTURE.