-1

I have an issue where I wish to get rid of some print, but it's ignored entirely and several lines are printed out. I have always used \r for these solutions. Is there a way round this scenario? (I'm using Mac OS for now, but guaranteed, will switch over to Windows soon.) @Feodoran told me that the \r only really works for Linux computers, which is an issue. The code for this is:

print ('virus_prevention.fix.virus.|attempt_enter')
time.sleep(2)
for i in range(1):
  sys.stdout.write(str(i)+'\r')
print ('virus_prevention.fix.virus|.attempt_enter')
time.sleep(0.1)
for i in range(1):
sys.stdout.write(str(i)+'\r')

And the output is the following:

virus_prevention.fix.virus.|attempt_enter
0
virus_prevention.fix.virus|.attempt_enter
0

I've tried sys.stdout.write rather than print but nothing seems to have changed. I'm not sure what to do in this situation, but if anyone could give me some tips please do. It's deleting the printed line which entirely does NOT work.

  • What are you trying to achieve? `for i in range(1):` only iterates once and is the same as it not being there at all. In what terminal are you invoking your python script (i.e. how it's being displayed can depend on what terminal is in use and under which OS (OS X uses \r for line shifts as you say) – MatsLindh May 30 '20 at 10:04
  • You are correct actually. But I'm still not clear on how to delete the print now. – Cubd Gaming May 30 '20 at 10:36
  • Does this answer your question? [Terminal - How to overwrite many lines?](https://stackoverflow.com/questions/27864992/terminal-how-to-overwrite-many-lines) There is a solution for MacOS, maybe it helps. – Georgy May 30 '20 at 10:45
  • Not quite. It just comes up with a blank screen. The 2nd may work. – Cubd Gaming May 30 '20 at 10:46
  • No, invalid syntax. – Cubd Gaming May 30 '20 at 10:46

1 Answers1

0
import time
import sys
sys.stdout.write('virus_prevention.fix.virus.|attempt_enter')
time.sleep(2)
for i in range(1):
    sys.stdout.write('\r')
    sys.stdout.flush()
    sys.stdout.write(str(i))
sys.stdout.write('virus_prevention.fix.virus|.attempt_enter')
time.sleep(2)
for i in range(1):
    sys.stdout.write('\r')
    sys.stdout.flush()
    sys.stdout.write(str(i))

Does this solve your problem?

Roshin Raphel
  • 2,285
  • 3
  • 12
  • 31