Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't sys.stdout.write('\b') backspace against newlines?

Tags:

python

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?

like image 295
brainysmurf Avatar asked Aug 29 '10 02:08

brainysmurf


People also ask

How do you overwrite stdout in Python?

Simple Version. One way is to use the carriage return ( '\r' ) character to return to the start of the line without advancing to the next line.

How do you not make a new line in Python?

The new line character in Python is \n . It is used to indicate the end of a line of text. You can print strings without adding a new line with end = <character> , which <character> is the character that will be used to separate the lines.

Does Python print add newline?

In Python, the built-in print function is used to print content to the standard output, which is usually the console. By default, the print function adds a newline character at the end of the printed content, so the next output by the program occurs on the next line.


2 Answers

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.

like image 59
John La Rooy Avatar answered Nov 15 '22 21:11

John La Rooy


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

like image 26
Ned Batchelder Avatar answered Nov 15 '22 19:11

Ned Batchelder