4

My script will run for a few hours and I want to be updated on its status. I usually use sys.stdout.write("\r"+string) and it works quite well but currently, I need to log multiple things, so I will have to use '\n' or add another print function.

I'm running out of ideas. I was playing around with curses but it can't find PyCharm's console when initscr().

So, how do you flush a multiline output?

Dummy code:

import sys
import time

for i in range(2):
    # sys.stdout.write(
    # "\r"+
    # "line 1 loop {loop1}\n"
    # "line 2 loop {loop2}\n".format(
    #     loop1=i,
    #     loop2=i
    # ))

    sys.stdout.write("\r" + "line 3 loop {loop}".format(loop=i))
    sys.stdout.write("\r" + "line 4 loop {loop}".format(loop=i))


    # sys.stdout.write("line 6 loop {loop}\n".format(loop=i))
    # sys.stdout.write("line 7 loop {loop}\n".format(loop=i))
    # sys.stdout.flush()

    # print("line 5 loop {loop}".format(loop=i), end="\r", flush=True)

    time.sleep(0.5)

Outputs:

line 4 loop 1

Desired output:

line 3 loop 1
line 4 loop 1

UPDATED since my example wasn't clear enough

import sys
import time

def increment(i):
    return i+1

def multiply(i):
    return i*2

for i in range(5):
    sys.stdout.write(
    "\r"+
    "candies eaten {candies}\n"
    "chocolates eaten {chocolate}\n".format(
        candies=increment(i),
        chocolate=multiply(i)
    ))

    time.sleep(0.5)

Outputs

candies eaten 1
chocolate eaten 0
candies eaten 2
chocolate eaten 2
candies eaten 3
chocolate eaten 4
candies eaten 4
chocolate eaten 6
candies eaten 5
chocolate eaten 8

Desired output

candies eaten 5
chocolate eaten 8

updated with each loop.

Tom Wojcik
  • 5,471
  • 4
  • 32
  • 44
  • I don't understand. You're using `\r`. This returns the cursor to the beginning of the line, overwriting the previous one. Use `string + '\n'`, or remove the `end` parameter from `print()`, and multiple lines will be printed. – salezica Jan 19 '18 at 13:55
  • @slezica I want old lines to be overwritten by the new lines. Can't figure out how to use both `\n` and `\r` in one line. – Tom Wojcik Jan 19 '18 at 13:59
  • But... the output you posted is correct if you wanted `line 4` to overwrite `line 3`. I'm not getting it, do you expect multiple lines or not? You can't _both_ overwrite _and_ get newlines, those are logically opposite requirements. What am I not getting? – salezica Jan 19 '18 at 14:07
  • By old lines, I meant the ones from the previous loop. My original output will look like this `"Downloaded = {}\nLeft = {}\nTime left = {}"` and more. I want each line to be replaced by the line from the next loop. – Tom Wojcik Jan 19 '18 at 14:11
  • Hey @TomaszWojcik did you end up finding a solution to this in the end for PyCharm? The question marked as a duplicate uses the console, not PyCharm – John Doe Jun 03 '20 at 06:56
  • Hey @JohnDoe. I agree it's not really a duplicate per se and I wasn't able to find a solution, if I remember correctly. – Tom Wojcik Jun 03 '20 at 07:22

0 Answers0