I have a program in Python that makes extensive use of the line feed character to produce the effect of an updating console line (specifically a progress bar). When trying to debug the code in PyCharm I saw that the progress bar doesn't get printed until it's done.
Upon further inspection it turned out that when a carriage return (\r
) is printed, the whole line is deleted. Because the library itself writes strings of the form ({line}\r
), I always get an empty line.
Sample code:
import sys
sys.stdout.write('xxx')
sys.stdout.flush()
time.sleep(1)
sys.stdout.write('\rZZ')
sys.stdout.flush()
time.sleep(1)
sys.stdout.write('yyy\r')
sys.stdout.flush()
time.sleep(1)
print ('===')
My run looks like this:
xxx
is printed
[After 1 second]ZZ
is printed
[After 1 second]- The line is deleted
[After 1 second] ===
is printed and the program terminates
This happens both in the debug and the run console when running this script.