2

So I have this code and I have no idea how to run it in only one line by just overwriting the previous output if you know what I mean. Here is the code:

import time

signs = ["|", "/", "-", "\\"]
while True:
    for i in range(4):
         print("\r" + signs[i].format(i))
         time.sleep(1)

I would be very pleased if you could help me

xenfoulis
  • 47
  • 1
  • 9
  • possible duplicate of https://stackoverflow.com/questions/255147/how-do-i-keep-python-print-from-adding-newlines-or-spaces or https://stackoverflow.com/questions/5598181/python-multiple-prints-on-the-same-line – rs311 Nov 16 '18 at 21:32

2 Answers2

3

You have to add end='\r' to your print statement, so this small change should do the job as you desired:

import time

signs = ["|", "/", "-", "\\"]
while True:
    for i in range(4):
         print(signs[i].format(i), end='\r')
         time.sleep(1)

By default it is \n so next line symbol, but with \r (carriage return) the current line will be overwritten by the next output.
It can cause problems though if the previous output is longer than the one following, then it does only overwrite as much characters as the new output has, the rest remains. In such cases it can help to add some whitespaces at the end of the output to fill the difference in sequence length.
Generally the behaviour is dependent on the used output i.e. Shell, Idle, writing to file, Eclipse, PyCharm etc. Some output shells may not support this command.

MBT
  • 21,733
  • 19
  • 84
  • 102
  • @FredLarson Yes, they are equivalent as I see it. – MBT Nov 16 '18 at 21:26
  • @stanisławsłowiński Are you testing it in the shell? For me it works fine in Jupyter, shell should work also fine. It may be that some outputs, like logs do not support overwriting. – MBT Nov 16 '18 at 21:28
  • Im using Pycharm – xenfoulis Nov 16 '18 at 21:29
  • It seems there can be issues with PyCharm: https://stackoverflow.com/questions/34950201/pycharm-print-end-r-statement-not-working – MBT Nov 16 '18 at 21:31
  • Problem solved, for some reaseon it only works in the terminal. Thats totally fine for me. Thanks for all the help! – xenfoulis Nov 16 '18 at 21:32
  • @stanisławsłowiński Your welcome! Would be glad if you accept the answer! – MBT Nov 16 '18 at 21:33
  • I also found that Idle doesn't seem to honor the `'\r'`, Eclipse console seems to treat it just like `'\n'`, and I had to flush to see anything in my Linux Bash console. – Fred Larson Nov 16 '18 at 21:41
  • Yes, unfortunately some output shells just don't process this command in a manner like - written is written! As said previously with log files it can be a problem too, because you cannot actually rewrite it. (As I know some readers are still able to simulate the desired behaviour on logs, I think `tail` does. So reading a log file with `\r` statements should work together with `tail -f`) – MBT Nov 16 '18 at 21:51
1

This will do it:

from __future__ import print_function # only needed for python 2.x
import time, sys

signs = ["|", "/", "-", "\\"]
while True:
    for i in range(4):
         print("\r" + signs[i].format(i), end="", flush=True)
         #print("\r" + signs[i].format(i), end="") # use this instead of above in python 2.x
         #sys.stdout.flush() # only needed for python 2.x
         time.sleep(1)
quant
  • 2,184
  • 2
  • 19
  • 29
  • 2
    I found that the future print_function in Python 2.7 [doesn't support `flush=True`](https://bugs.python.org/issue28458). – Fred Larson Nov 16 '18 at 21:47
  • @FredLarson In this case you need `import sys` and then `sys.stdout.flush()` ... Thanks for pointing this out. I updated my answer accordingly! – quant Nov 16 '18 at 21:49