Print Status in a single line

Asked

Viewed 132 times

1

It is possible to make a status report of the script via terminal?

For example, I have a function called test, before it is executed, the script displays in the terminal:

'Run test ################################################################################## ]'

And after the execution of the function steps, it would replace the Loading by 'OK' getting:

'Run test ################################################################################## ]'

How to do this or something similar to this in Python?

  • It is not duplicated no. What he wants is to know how to print and change the text on the same line.

  • 2

    @Jeanextreme002 And that is exactly what is discussed in the other question. There I even say how to circumvent the limitation of \r when the string is smaller than the previous one. You might want to stop by and see.

  • Vish, sorry. I hadn’t read the right answer to the other kkkk question. It really is duplicated, I ask forgiveness.

1 answer

0

You do. Use the \r at the beginning of your string. What this special character does is replace the text from the beginning of the current line with a new one. Example:

import time
sprites = ["\\ ","| ","/ ","--"]
i = 0

while True:
    print("\r Loading... %s"%sprites[i],end="")
    time.sleep(0.3)
    i += 1
    if i >= len(sprites):
        i = 0

Like I said, the "\r" replace the text from the beginning with a new one, but the rest of the text remains the same. So if I run this code here:

print("\r Como você está ?",end="")
print("\r Estou bem.",end="")
input()

The final result will be:

Estou bem.está ?

That’s because the second print did not have enough characters to replace the rest of the text.

  • In this case, I wanted to do something like this: Function 1 ############################################/' here would be OK or not OK depending on if Return/' ]

  • 1

    I find it strange that you insist that the question is not duplicated, and in the end, write an answer with the same solution that is in the answer given. (You can add your answer there. You can answer here just because you believe it is simpler. But click there, read the answer, and write the same thing back, think kind of over)

  • Vish, sorry. I hadn’t read the right answer to the other kkkk question. It really is duplicated, I ask forgiveness.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.