0

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.

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
Manas Hejmadi
  • 63
  • 1
  • 8

1 Answers1

0

The progress bar will only be displayed in a terminal.

https://click.palletsprojects.com/en/7.x/api/#click.progressbar

By default, this progress bar will not be rendered if the file is not a terminal.

Håken Lid
  • 22,318
  • 9
  • 52
  • 67
  • Do you have any idea how to enable storing of the output in realtime. i.e every change in the cmd output should get changed in the variable in realtime. – Manas Hejmadi Oct 15 '18 at 16:41
  • Don't know. But there are many other questions about this. https://stackoverflow.com/questions/17411966/printing-stdout-in-realtime-from-a-subprocess-that-requires-stdin – Håken Lid Oct 15 '18 at 16:48
  • Thank you so much sir! Youve helped me a lot god bless! – Manas Hejmadi Oct 15 '18 at 16:50