When writing carriage return to a pycharm console the whole line is deleted?

前端 未结 3 1992
暖寄归人
暖寄归人 2020-12-11 07:13

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

3条回答
  •  盖世英雄少女心
    2020-12-11 07:43

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

    Print each line with the carriage return at the start, and without the default end='\n'. Didn't need the flush, though I didn't do much testing.

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

    Continue like this...

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

    To keep the last printout, keep the default end.

    print('\r===')
    

    Hope this works for you.

    Frank

提交回复
热议问题