Refresh console line [duplicate]

1

I have a loop in python, and I would like to report the value of a variable every time it is updated, but I do not want to dirty the console giving print every time < the console .

Is there a way for me to get this result?

print("Executando processo:")
while True:
    a = a +1
    print a

The result at the end of 3 interactions is:

Interaction 1:

$ - Executando processo: 
$ - 1 

Interaction 2:

$ - Executando processo:
$ - 1 
$ - 2

Interaction 3:

$ - Executando processo:
$ - 1 
$ - 2
$ - 3

And I would like to get the following result:

Interaction 1:

$ - Executando processo:
$ - 1 

Interaction 2:

$ - Executando processo:
$ - 2

Interaction 3:

$ - Executando processo:
$ - 3 
    
asked by anonymous 09.06.2017 / 22:47

1 answer

1

Uses the carriage return character \r .

import time

print('=== Executando ===')

for i in range(1, 11):
    print('- Processo:', i, end='\r')
    time.sleep(1)  # só para dar tempo de ver a mudança no console

print('=== Encerrado ===')

link

  

... causes a printer or output device (usually the monitor) to move the cursor to the first position of the line where it is located.

    
09.06.2017 / 23:20