Ciasto piekarz Ciasto piekarz - 2 months ago 10
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 = 'https://api.thingspeak.com/update?api_key=%s' % myAPI
print baseURL

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

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


sleep(int(myDelay))
except:
print 'exiting.'
break


Can anyone please through some light ?

Answer

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()