How to add a text progress bar in terminal with block characters with Python?

Sometimes, we want to add a text progress bar in terminal with block characters with Python.

In this article, we’ll look at how to add a text progress bar in terminal with block characters with Python.

How to add a text progress bar in terminal with block characters with Python?

To add a text progress bar in terminal with block characters with Python, we can use sys.stdout.write to write block characters to the screen.

For instance, we write

import time
import sys

for i in range(100):
    time.sleep(1)
    sys.stdout.write("r%d%%" % i)
    sys.stdout.flush()

to call sys.stdout.write to write the percentage number to the screen with "r%d%%" % i.

And then we call flush to flush out the output.

Conclusion

To add a text progress bar in terminal with block characters with Python, we can use sys.stdout.write to write block characters to the screen.