I've got the following piece of code:
def progressbar(count, total, status=""):
bar_len = 40
filled_len = int(round(bar_len * count / float(total)))
percents = round(100.1 * count / float(total), 1)
bar = "X" * filled_len + "-" * (bar_len - filled_len)
print("[{}] {}{} ...{}".format(bar, percents, "%", status),
end="\r", flush=True)
And for calling the progress bar:
total = 100
i = 0
while i < total:
i += 1
progressbar(i, total, status="Creating stuff")
time.sleep(1)
Where total is a number of iterations. When I run this code I get the progress bar running on multiple lines instead of just one. Any advice?
I don't think you have any issue in your code.
print("[{}] {}{} ...{}".format(bar, percents, "%", status),
**end="\r"**, flush=True)
the end="\\n" argument in print() method should print the progress bar i one line.
if you run this code in terminal it should work fine.
I have previously seen an issue with PyCharm IDE where end="\\r" doesn't work. It is probably a bug in the terminal emulator in the IDE.
Thanks for everyone that commented on my topic trying to help. I got the solution on another StackOverflow Question: Python 3 progress bar shows nothing
So I tested two ways that were successful. The first one, to print using sys.stdout:
sys.stdout.write("\r[{}] {}{} ...{}".format(bar, percents, "%", status))
sys.stdout.flush()
And the second, using the print() but with the \\r in front of the line:
print("\r[{}] {}{} ...{}".format(bar, percents, "%", status),
end="", flush=True)
Both works perfectly. The first one needs the import sys.
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.