Create a progress bar in Python using carriage return
table of contents
My name is Saito, an infrastructure engineer.
Today I would like to help everyone learn about Carriage Return (CR) and write some scripts.
Then, try using carriage return in Python and creating a progress bar.
What is carriage return (CR)?
Carriage return (CR) is a special symbol represented by r. Many people may know the newline character n.
What is a carriage return?
Originally, when using a typewriter, the action of manually returning the part called the carriage that prints characters is called carriage return (CR).
The action of moving to a new line is called line feed (LF).
All operating systems in use to this day have a history of being compatible with electronic typewriters, and this 'r' appears in some form.
For example, in Windows OS, line feed is rn. This can be interpreted as "return to the original position (CR) and then move to a new line (LF)".
Unix, Mac, Linux, BSD, etc. use r and n separately based on the POSIX standard.
When you use the newline character n, the cursor is set to the beginning of a new line.
In other words, n has the role of both CR and LF. On the other hand, r has the role of CR, which returns the cursor to its original location.
Try using r
I gave examples for each OS, but even in programming languages, r and n are often used separately.
Let's actually use r in python.
$ python -c "import sys; sys.stdout.write('beyondrBn')" Beyond
- "-c" is an option for writing one-liners in python.
- Standard output is performed using sys.stdout.write, and the string is written to the buffer (cache).
Now, how the display results are determined in 'beyondrBn'.
1. Introduction string displays beyond. At this time, the cursor is after d.
2. Since there is r, the cursor moves to the beginning of b.
3. This time, cover B with b and print.
4. There is a new line because there is n.
In this way, r can write characters on the same line many times.
Create a progress bar
You can use the above properties to create a progress bar that runs on the command line. Below is a progress bar written in python.
import sys, time def prog_bar(length=100): for i in range(length): sys.stdout.write('#'*i + 'r') sys.stdout.flush() time.sleep(0.01) for i in range(4): prog_bar()
sys.stdout.write is cached before displaying on the terminal.
(Actually, print() uses sys.stdout internally.)
By using sys.stdout.flush, cached information is deleted.
Try writing an animation
Let's also write a fun animation of the spinning bar. (The following has been confirmed to work with python3)
import time space = '.' bar = '-|/' length = 100 printset = (('{0}{1}r'.format (space*(i-length//2) if i > length//2 else '', bar[i%4]), time.sleep(0.05)) for i in range(length)) for i in printset: print(i[0], end='') print(' '*length , end='r')
It can also be used for various other purposes.
I'm also adding slot-like animation to the password generation script.
Please expand and play with it.
If you want to consult a development professional
At Beyond, we combine our rich track record, technology and know-how in system development that we have cultivated to date with OSS technology and cloud technologies such as AWS to create contract development of web systems with reliable quality and excellent cost performance.
We also work on server-side/backend development and linkage development of our own APIs, using the technology and know-how of the construction and operation of web system/app infrastructure for large-scale, highly loaded games, apps, and digital content.
If you are having trouble with development projects, please visit the website below.
● Web system development
● Server-side development (API/DB)