1

I was trying to make my script write a percentage that updates, and I ran into the following problem: I need Python to erase the last bunch of characters I printed, but I can't get it to erase more than 2 characters.

Entering this in Jupyter Notebook

sys.stdout.write("abcd")
sys.stdout.write("\b")

outputs

abc 

which is fine, let's erase one more

sys.stdout.write("abcd")
sys.stdout.write("\b\b")

outputs

ab

very well

sys.stdout.write("abcd")
sys.stdout.write("\b\b\b")

outputs

abc

what? What happened? why does it erase just one character instead of 3? What can I do to avoid this?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
user2723984
  • 113
  • 5
  • @rdas that's utterly obvious and counterintuitive at the same time, thanks. Is there no way I can erase 3 characters then? – user2723984 May 05 '20 at 15:47
  • 1
    @rdas wait, then why \b\b works? shouldn't the second \b erase the first one? Instead it correctly erases 2 characters! – user2723984 May 05 '20 at 15:49
  • The fact that this does not output `abcd` in both cases surprises me. What do you write to `sys.stdout` *after* the `\b`'s? – mkrieger1 May 05 '20 at 15:54
  • @mkrieger1 nothing, that is all I write – user2723984 May 05 '20 at 15:55
  • Does this answer your question? [Please explain this unexpected \b (backspace) behavior](https://stackoverflow.com/questions/40251434/please-explain-this-unexpected-b-backspace-behavior) – mkrieger1 May 05 '20 at 15:55
  • Do you type these commands in an interactive session, or are they in a script? – mkrieger1 May 05 '20 at 15:56
  • Sorry, I'm not sure what you mean, I'm using a Jupyter notebook – user2723984 May 05 '20 at 15:57
  • I mean, do you type `sys.stdout.write(...)` and then press Enter, and then the output appears, or do you write `sys.stdout.write(...)` to a file, save the file, and then run the file by clicking a "run" button or executing `python thefile.py` from the command line? – mkrieger1 May 05 '20 at 15:59
  • @mkrieger1 the former – user2723984 May 05 '20 at 16:00
  • In any case, as explained in the answers to the other question, `\b` does not *erase* anything, it only moves the cursor to the left, and what appears as output depends on what is written afterwards. – mkrieger1 May 05 '20 at 16:01
  • @mkrieger1 I don't think I'm having the same problem as in that question, for example sys.stdout.write("abcd") sys.stdout.write("\b\b\b") sys.stdout.write("fgh") outputs abcfgh, while as per that explanation I'd expect afgh – user2723984 May 05 '20 at 16:02
  • @mkrieger1 and if \b doesn't erase anything, but merely shifts the cursor to the previous character, then why do my first two example work? I'm very confused – user2723984 May 05 '20 at 16:05
  • 1
    I suspect that Jupyter Notebook writes something to `sys.stdout` by itself, but I'm not sure. – mkrieger1 May 05 '20 at 16:05
  • Does this answer your question? [The "backspace" escape character '\b': unexpected behavior?](https://stackoverflow.com/questions/6792812/the-backspace-escape-character-b-unexpected-behavior) – Tom Carrick May 05 '20 at 18:41
  • @TomCarrick how does it explain that 3 backspace characters have the same effect as 1? (I'm not trying to sound as offensive as it reads, sorry) – mkrieger1 May 05 '20 at 19:10
  • @user2723984 I expect mkrieger1 is correct and it's an issue with how jupyter intercepts and manages its internal sys.stdout, because with a "raw" stdout using the normal python repl in a regular terminal `\b` behaves as expected (it just moves the cursor backwards at which point it's possible to overwrite the line content, and `"abcd\b\b\bfgh"` does indeed result in an output of `afgh`). – Masklinn May 06 '20 at 07:47

0 Answers0