Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I overwrite/print over the current line in Windows command line?

On Unix, I can either use \r (carriage return) or \b (backspace) to overwrite the current line (print over text already visible) in the shell.

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.

like image 854
Aaron Digulla Avatar asked Jan 21 '09 13:01

Aaron Digulla


2 Answers

yes:

import sys import time  def restart_line():     sys.stdout.write('\r')     sys.stdout.flush()  sys.stdout.write('some data') sys.stdout.flush() time.sleep(2) # wait 2 seconds... restart_line() sys.stdout.write('other different data') sys.stdout.flush() 
like image 140
nosklo Avatar answered Sep 22 '22 09:09

nosklo


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:

>>> from time import sleep >>> for i in range(400): >>>     print("\r" + str(i), end="") >>>     sleep(0.5) 

EDIT: It works on Windows and on Ubuntu

like image 20
adho12 Avatar answered Sep 23 '22 09:09

adho12