On Unix, I can either use \r (carriage return) or \b (backspace) to print over text already visible in the shell (i.e. overwrite the current line again).
Can I achieve the same effect in a Windows command line from a Python script?
I tried the curses module but it doesn't seem to be available on Windows.
yes:
I just had this problem. You can still use
\r
, even in Windows Command Prompt, however, it only takes you back to the previous linebreak (\n
).If you do something like this:
You'll get:
That's because
\r
only goes back to the last line. Since you already wrote a newline character with the last print statement, your cursor goes from the beginning of a new empty line to the beginning of the same new empty line.To illustrate, after you print the first 0, your cursor would be here:
When you
\r
, you go to the beginning of the line. But you're already on the beginning of the line.The fix is to avoid printing a
\n
character, so your cursor is on the same line and\r
overwrites the text properly. You can do that withprint 'text',
. The comma prevents the printing of a newline character.Now it will properly rewrite lines.
Note that this is Python 2.7, hence the
print
statements.easy method :)
And if on the IPython-notebook, just like this:
http://nbviewer.ipython.org/github/ipython/ipython/blob/master/examples/notebooks/Animations%20Using%20clear_output.ipynb
Simple way if you're just wanting to update the previous line:
I know this is old, but i wanted to tell my version (it works on my PC in the cmd, but not in the idle) to override a line in Python 3:
EDIT: It works on Windows and on Ubuntu