1

I'm writing a python script where I print a variable from a list. At the moment, I am using print("Variable: ", var, "\n") which works but it fills up the terminal. I'd like it to only update the variable and not the text so that it stays on one line.

I tried importing os and adding os.system('clear') to the end of the loop, but I found it flashes and is unreadable. This is somewhat what I have:

import jquery
import os

varList = jquery.loads(open('variables.json').read())

for varCurrent in varList:
    print ("Variable: ", varCurrent, "\n")
    # Clear the terminal
    #os.system('clear')

Although it works, this is completely unreadable due to the flash when the terminal clears.

Desired output: Variable: $var where $var updates and "Variable: " does not. Meaning no new lines and no terminal clearing.

Tokumei
  • 13
  • 3
  • please write the desired output in a clean way so we can assist you to achieve it – Gulzar Aug 20 '19 at 11:16
  • https://stackoverflow.com/questions/9949887/delete-characters-in-python-printed-line write('\b') # <-- backup 1-character but not in all terminals its working, cause its a console reation on symbol – snamef Aug 20 '19 at 11:19

1 Answers1

2

Like on a typewriter, you should do a carriage return with the '\r' symbol:

import time

for x in range(10):
    print(f'\rVariable: {x}', end='')
    time.sleep(.5)
ForceBru
  • 43,482
  • 10
  • 63
  • 98