Updating a line in terminal with python [duplicate] - python

This question already has answers here:
Output to the same line overwriting previous output?
(11 answers)
Closed 6 years ago.
I'm not sure where to start with this, so even pointing me in the right direction would be helpful. I would like to update a line continuously in the terminal using python but can't figure out how to do this. I'm thinking something like top is doing with constantly updated information but not printing new lines.
So simplistically something like this:
for i in myList:
print i #but overwrite previous output rather than putting on new line

You can print a '\r' (which places the caret at the beginning of the current line) after any output:
for x in range(10):
print x, '\r',
print "\n"
You can also add sys.stdout.flush() after printing to make the output visible immediately.

Related

How To Update a Line Without Printing a new One in Python? [duplicate]

This question already has answers here:
How to print without a newline or space
(26 answers)
Closed last year.
I am trying to make a progress bar, but I don't want it to print a new line for each character that represents the bar (like: "#"). How do I update the line without printing it again?
This is not direct answer to question but just potential suggestion. for many "pretty" terminal formatting i have found rich python package to be very useful. for example has progress bar: link see example of what looks like in gif under progress bar section of readme
You can print a # without newline with:
print('#', end='', flush=True)
You need to change the end of line character to nothing.
print("#", end =''', flush=True )

Outputting argument but no new line feature in Python [duplicate]

This question already has answers here:
How to print without a newline or space
(26 answers)
Closed 1 year ago.
Is there a way how to remove the property of the print() function in Python that makes end of line after outputting its argument? Or is there a different function to do the output of argument but no the end line?
To illustrate this, lets say I want to output the English alphabet on one line. The following code prints every letter on a new line:
for x in range(26):
print(chr(x+97))
While the second code prints it the way requested, but by concatenation of strings instead:
s=''
for x in range(26):
s+=chr(x+97)
print(s)
print(“Hello”, end=“”)
print(1)
Output: Hello1
Hope this helps :) the default value of end is “\n” and you can change it to anything you want.

Removing printed lines in Python 3 [duplicate]

This question already has answers here:
Replace console output in Python
(12 answers)
Closed 4 years ago.
I am trying to build a game.
The game will have an item called a "pulsating crystal" (I am using \033[1;31;40m] to change the items colour), I want to it to be rainbow, so it keeps changing colours, without deleting everything else in the terminal. I used print(\033c) to clear the terminal but I just want to print the last line. I am sorry if the question is unclear or repetitive, or has another answer but I couldn't find another clear answer for my problem. PS I use Linux.
I just want to print the last line.
To print a line repeatedly, just override the line ending \n by giving the keyword argument end='\r' to print().

How to make it look like the computer is typing? [duplicate]

This question already has answers here:
How to print one character at a time on one line?
(4 answers)
Closed 7 years ago.
I want it to look like the computer is trying to type back to the user. I have tried some code, but when I run it, it just prints everything at 1 time even though I am printing it 1 at a time.
A = "Random sentence"
for x in A:
time.sleep(random.randint(1,4))
print(x, end='')
You need to use stdout.flush(). This will force it to show up after every print.
import sys
A = "Random sentence"
for x in A:
time.sleep(random.randint(1,4))
print(x, end='')
sys.stdout.flush()

How can I show never ending dots ("...") in a while loop [duplicate]

This question already has answers here:
Loading animation in python
(3 answers)
Closed 7 years ago.
Is it possible to show something like this in Python?
Checking.
Checking..
Checking...
Checking.
I want it to just show one line of this while the script is running, but stop when it's done. The script I want to add it to is here: https://github.com/brandonusher/Python-Scripts/blob/master/check_port.py
Output the text with no newline character, then flush stdout. Output a \r to make the cursor go back to the beginning of the line. Repeat until done. Don't forget to overwrite existing text.
while True:
sys.stdout.write('\rfoo. ')
sys.stdout.flush()
delay(100)
sys.stdout.write('\rfoo.. ')
sys.stdout.flush()
delay(100)
sys.stdout.write('\rfoo...')
sys.stdout.flush()
delay(100)

Categories

Resources