When moving a carriage to the pycharm console, does the entire line get deleted?

I have a Python program that makes extensive use of the line character to create the effect of an update console line (in particular, a progress bar).

When trying to debug code in PyCharm, I saw that the progress bar does not print until it finishes.

Upon further inspection, it turned out that when the carriage return returns ( \r ), the entire line is deleted.

Since the library itself writes form lines ( {line}\r ), I always get an empty line.

Is there a way to solve this problem using PyCharm? Currently, what I will do is replace stdout version that writes the current line and retypes it after receiving a carriage return. However, I would have a pretty simple way to do this.

Code example:

 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 launch looks like this:
1. "xxx" is printed
[After 1 second]
2. "ZZ" is printed
[After 1 second]
3. Line deleted
[After 1 second]
4. '===' is printed and the program terminates

This happens both in the debug console and in the launch console when the script is run.

+3
python carriage-return pycharm console-application
source share
3 answers

I recently ran into the same problem and found a solution. The answer is actually in your post. As you said, carriage return deletes the entire line. To avoid the problem, print the carriage return only when printing a new line:

Print each line with a carriage return at the beginning and without the default value end = '\ n'. I do not need a flash, although I have not done many tests.

 print('\rxxx', end='') # sys.stdout.flush() time.sleep(1) 

Go on like this ...

 print('\rZZ', end='') time.sleep(1) print('\ryyy', end='') time.sleep(1) 

To save the last printout, keep the default value.

 print('\r===') 

Hope this works for you.

Franc

+4
source share

Be careful with the short time between prints and the short length of printed lines; printing can be complex and print multiple values ​​on one line.

To get around this, you can add a second \ r after the white character:

 for i in range(10): print("\r \r{0}".format(str(i)), end='') time.sleep(0.1) 

My solution for a similar problem is: https://stackoverflow.com/a/316677/

+1
source share

The error is still active and reported here . Currently, if you use Run> Configuration> "Emulate Terminal in Output Console", carriage return will work as expected.

0
source share

All Articles