Ciasto piekarz Ciasto piekarz - 3 months ago 13
Python Question

clearing last dumped text from terminal by flushing stdout

Hi I kind of forgot in past I remember having done clearing out the last printed text in terminal doesnt seems to work how I am doing now

def main():

print 'starting...'

baseURL = '' % myAPI
print baseURL

while True:
RHW, TW, TWF = getSensorData()
# LT = RCtime(RCpin)
f = urllib2.urlopen(baseURL +
"&field1=%s&field2=%s&field3=%s" % (TW, TWF, RHW))

sys.stdout.write("Celcius: %s , Farenheit: %s , Humidity: %s "% (TW, TWF, RHW))

print 'exiting.'

Can anyone please through some light ?


if you actually want to clear the screen you should use curses or ncurses

some hacks however are

  • \b backspace character ... allows you to write over the last character

  • \r carriage return without newline ... this will move the curser to the start of the current line in most terminals (allowing you to "overwrite" the current line) ...

  • there are also some crazy hex escape codes that are very terminal specific

os.system('cls') or os.system('clear') will sort of work depending on your operating system

if you just want to flush the current stdout that is simply sys.stdout.flush()