Why doesn't sys.stdout.write('\\b') ba

2020-08-23 05:18发布

问题:

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?

回答1:

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.



回答2:

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



回答3:

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.



标签: python