简体   繁体   中英

remove the last written line and replace it python

In order to create a progress bar that show the lode by present

The thinking behind it was to erase the line and then rewrite it so it will look like the current precent.

I tried to do this:

import time
---some kind of loop---:
    ... downloading stuff ...
    b = str((prhelp / 576)*100)
    print(b + "%", end="\r")
    time.sleep(1)
    prhelp = prhelp + 1

But it just don't printing any thing

Also I would like to know how to print a number only 3 digits after the dot - 12.345678 --> 12.345

So my program won't print too long precent

it's because when you print , you give the parameter end is the string that will be added to the end. By default, it's \\n . You gave \\r . What this character means is remove the current line . That's why it doesn't print a thing...

If you want it to work, your replace this:

print(b + "%", end="\r")

by:

print(b + "%")

What I'm guessing you want is:

import time
print('Hello', end='')
time.sleep(1)
print('\rworld!')

This code will print Hello , wait 1 second, and then replace the Hello by world!

At the end I just needed to do:

import time
b = str(round((prhelp / 576) * 100,3))
print('\r',end = "")
print(b + "%",end = "")
time.sleep(0.01)
prhelp = prhelp + 1

By doing

print('\r',end = "")

I'm removing the last line and makeing place for the next number im writing.

Thanks for all the helpers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM