10

Compare:

for item in range(0, 5):
    sys.stdout.write('c')
for item in range(0, 5):
    sys.stdout.write('\b')

Works as you would imagine, but:

for item in range(0, 5):
    sys.stdout.write('\n')
for item in range(0, 5):
    sys.stdout.write('\b')

still leaves you with five newline characters. Any ideas?

brainysmurf
  • 646
  • 1
  • 7
  • 16
  • 2
    Aside: you could also use `'\b'*5` to [create a string of 5 `\b` characters](http://stackoverflow.com/a/1424016/1174169) instead of iterating over a range. – cod3monk3y Feb 19 '14 at 15:33

3 Answers3

17

It may seem reasonable today to expect backspace to be able to work over newline characters, on a console but that would not be backward compatible with teletypes as there is no reverse linefeed.

John La Rooy
  • 295,403
  • 53
  • 369
  • 502
  • 3
    this made me nostalgic for the clatter of a decwriter, for only about 120 ms though. – msw Aug 29 '10 at 03:07
  • In light of this, some may find [this answer](http://stackoverflow.com/a/517207/1174169) useful for a simple way to rewrite a line in stdout. Synopsis: `sys.stdout.write('ccccc\r')` which saves you knowing how far to back up if you want to just go all the way back to the newline. – cod3monk3y Feb 19 '14 at 15:29
4

This is about the behavior of console windows: backspaces only work within a line, they won't backup over newlines.

Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
0

This is absolutely nothing to do with Python. It's your console driver that handles any visual effects. Most of them will emulate an ASR33 teletype ... backspace means move the print head one space back towards the start-of-line position, if possible.

John Machin
  • 81,303
  • 11
  • 141
  • 189