3

I tried to update percent on the same line or the same two line by using print , rather than printing over and over again.

I have already found some approaches for flush output on one line, but no one for multilines.

Below is my code, But they are not working as expected, Could someone help me?

# refresh in one line
for i in range(10):
    print("Task A complete %d, Task B complete %d \n" % (i, i), end=" ", flush=True)

# refresh in two line
for j in range(10):
    print("Task A complete %d \n" % j,  end=" ", flush=True)
    print("Task B complete %d \n" % j,  end=" ", flush=True)

Thank you all in advance

Update:

with code

# refresh on one line
for i in range(1000000000):
    print("Task A complete %d" %i, "Task B complete %d" %i, end='\r', flush=True)

get output like:

Task A complete 994      Task B complete 993

But How can I break this line in two lines like:

 Task A complete 994      
 Task B complete 993
sun0727
  • 374
  • 4
  • 19
  • 1
    Well there are two things here: (a) you write a new line '\n'` yourself, and (b) you do not return to overwrite. – Willem Van Onsem May 30 '18 at 21:09
  • 1
    To overwrite, you probably wanted `end="\r"`, not `end=" "`. (And of course you have to not end the actual string with `\n`.) – abarnert May 30 '18 at 21:13
  • 1
    It's also worth noting that, even if you fix that, it won't overwrite smoothly unless every output line is the same length. For example, try `print('abcde\r') then `print('aaa\r')`—you'll see `aaade`, not `aaa`. – abarnert May 30 '18 at 21:14
  • 1
    `str.ljust()` is handy for making sure you overwrite the whole line. – kindall May 30 '18 at 21:26
  • @abarnert I have seen what you said.. Are there anyway to solve this? – sun0727 May 30 '18 at 21:50
  • You have all you need in the comments above: get rid of the `\n` as Willem Van Onsem explained, use `\r` instead of space as I explained, and look into the `ljust` method as `kindall` explained. What are you having trouble with? – abarnert May 30 '18 at 22:07
  • @abarnert I have updated my code and trouble... Thanks for your help. I am noob in python .. – sun0727 May 30 '18 at 22:46
  • 2
    Your new question is asking for something impossible. There's no way to print two lines and then overwrite both of them. If you want to get fancier, you _can_ do the equivalent of that by outputting console escape sequences, or by using a library that does that for you, or by going all-out and using the `curses` library (on *nix) or a third-party `conio` library (on Windows) to take over the whole terminal display… but that isn't as simple as what you were trying to do, and it may not be appropriate depending on your intended use case. – abarnert May 30 '18 at 22:48
  • Meanwhile, even for a single line, there are two problems with your code: (1) you need to call `ljust` on the string you get back from formatting in the value, not from the format string, and (2) you need to call `ljust` on both halves of the line, not just the first half, or you end up with the same problem you started with. – abarnert May 30 '18 at 22:50
  • I think escape sequences will be good for it – Andrii Maletskyi May 30 '18 at 22:54
  • @AndriyMaletsky If you only want to work on a single terminal type, maybe. But doing it in a way that works on Windows, xterm, vt100, vt52, and oddball terminals you've never heard of is not so trivial. – abarnert May 30 '18 at 22:55
  • 2
    [Here's an example using `curses`](https://gist.github.com/abarnert/dd68718779ba81b93c31ff8bcbb35153). It takes over (and clears) the whole screen, and it doesn't work on Windows—but, on the other hand, it works on any workable terminal, avoids breaking scrollback and other fancy terminal features, etc. – abarnert May 30 '18 at 23:05
  • @abarnert: Are you specifically meaning in it's impossible in Python? [I've done something similar in Bash using nothing more than a little trickery inside a `for` loop](https://stackoverflow.com/a/43478958/499581). – l'L'l May 31 '18 at 01:25
  • 1
    @l'L'l You're using the `tput` command. That does exist on macOS and most linux distros, but it was only added to POSIX in 2003, and is optional—and, of course, won't exist at all on Windows. You can get third-party libraries on PyPI that do the same thing in Python in an even more portable way, but still not for Windows. Or, of course, you can `subprocess` out to `tput` and do the exact same thing as your `bash` script. But, either way, you can't do it just by printing characters in a portable way, in either bash or python. – abarnert May 31 '18 at 01:37
  • 2
    @l'L'l That being said, using `tput` is much nicer than the usual answer people give of "just stick a `\033[F` in your string" (yours checks `isatty`, does the right thing if `$TERM` has different termcaps, and of course is a lot more readable). But it's still not the right answer for someone who hasn't even told us whether he's on Windows. – abarnert May 31 '18 at 01:43

0 Answers0