Get the App
SLTechnology News&Howtos  ›  Development  › 

How to implement a progress bar in python

Shulou Source: shulou.com Published: 2022-06-01 05:42:45 10月03日 Update

This article mainly introduces python how to achieve a progress bar, the article is very detailed, has a certain reference value, interested friends must read it!

Implement a progress bar from time import sleepdef progress (percent=0, width=30): left = width * percent / / 100right = width-left print ('\ r [','#'* left,'* right,']', f' {percent:.0f}%', sep='', end='', flush=True) for i in range: progress (I) sleep

Display effect

The print in the above code has several useful parameters. The role of sep is what is the delimiter. The default is a space. Here, it is set to an empty string to make each character more compact. What is the end of the end parameter? the default is the enter newline character. Here, in order to achieve the effect of the progress bar, it is also set to an empty string. There is also the last parameter flush, which is mainly used to refresh. The default is flush = False. If it is not refreshed, the contents of print to f will be stored in memory first; when flush = True, it will immediately refresh and output the contents.

The above is all the contents of the article "how to achieve a progress bar in python". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Tags: Progress content parameters effect effect empty string article compact between code value interest memory delimiter character buddy buddy newline character more useful Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Huawei MariaDB Shulou Tech Info Linux