I'm new to python programming and using ubuntu to do so. In a program I have done I used a delay of 1 minute until it executes the code again. How can I program a timer to be displayed in the terminal based on the value of the delayed time? Thanks in advance...
The simplest way is as follows.
import time
import sys
for remaining in range(10, 0, -1):
sys.stdout.write("\r")
sys.stdout.write("{:2d} seconds remaining.".format(remaining))
sys.stdout.flush()
time.sleep(1)
sys.stdout.write("\rComplete! \n")
"\r" returns the text cursor to the beginning of the line, so that you can overwrite what you're previously written. Because typically output isn't written until a newline ("\n"), you need to manually .flush() the output stream.
Because the line isn't being cleared, we need to ensure that each new line of output is long enough to cover up the existing line.
The curses module has tools for more advanced terminal output, but is more complicated to use.
Related
Printing text that updates, for example a clock, in python. Ive read some threads about this but they only worked if the program prints out one line. What i am trying to do is, i already have a sort of terminal in python that accepts commands and executes them, it runs in a while loop, now if i wanted to make a clock in the top right corner of the terminal, but preserve what the user is typing and the output of previous commands, how would i do that?
P.S.: Sorry for bad formatting, in typing this from my phone
You can easily update stdout with this:
print('foo', end='')
print('\rbar', end='', flush=True)
This example prints out foo and then changes the line to bar
I'm having trouble with the Terminal on my Mac.
I'm trying to print out delay text (like it appears on a type writer) and the code (below) was correct when I tested on an online compiler.
import sys
import time
intro1= "Welcome player. What's your name?"
for x in intro1:
sys.stdout.write(x)
time.sleep(0.2)
But my Mac Terminal just freezes for a sec and print out the whole statement in one go. I got Python 2.7.10 on the Mac. I looked up online and I think my Terminal is cutting the buffer for convenience's sake but now I actually need the buffer (I'm going to be printing out delayed text a lot). Is there any statement to turn on the buffer (or fix it since it should be set by default) on my Terminal? Thanks a lot
You need to add sys.stdout.flush() after each sys.stdout.write() to force the character out instead of letting it get buffered:
import sys
import time
introduction = "Welcome player. What's your name? "
for character in introduction:
sys.stdout.write(character)
sys.stdout.flush()
time.sleep(0.2)
name = raw_input()
I am writing a program to simulate typing and it runs in the python shell but not when double clicked any ideas?
My code is as follows:
import sys,time
def slow_text(str):
for letter in str:
sys.stdout.write(letter)
sys.stdout.flush
time.sleep(0.1)
print("")
slow_text('Hello')
I am using python 3.5.
You're not actually calling sys.stdout.flush. That line should be:
sys.stdout.flush()
Without flushing, what's actually happening is that the script delays for some seconds with a blank console window (while the characters go into the output buffer) and then they all appear at once and the script ends and the window immediately closes, before you have a chance to see them.
That it worked in the Python shell was just a coincidence.
I was writing a simple program on Python 3.1 and I stumbled upon this:
If I run this on the IDLE it works as intended - prints "Initializing." and then adds two dots, one after each second, and waits for input.
from time import sleep
def initialize():
print('Initializing.', end='')
sleep(1)
print(" .", end='')
sleep(1)
print(" .", end='')
input()
initialize()
The problem is that when I double-click the .py to execute the file, it runs on python.exe instead of pythonw.exe, and strange things happen: it joins all the sleep() times i.e. makes me wait for 2 seconds, and then prints the whole string Initializing. . . at once. Why does this happen? Is there a way to avoid that happening in the terminal? It works fine if I use the IDLE in both windows and linux.
This is because the output is being buffered.
You should add a sys.stdout.flush() after each write
It sounds like the difference is that stdout is automatically being flushed in IDLE. For efficiency, programming languages often save up a bunch of print calls before writing to the screen, which is a slow process.
Here's another question that has the answer you need:
How to flush output of Python print?
I was writing a simple program on Python 3.1 and I stumbled upon this:
If I run this on the IDLE it works as intended - prints "Initializing." and then adds two dots, one after each second, and waits for input.
from time import sleep
def initialize():
print('Initializing.', end='')
sleep(1)
print(" .", end='')
sleep(1)
print(" .", end='')
input()
initialize()
The problem is that when I double-click the .py to execute the file, it runs on python.exe instead of pythonw.exe, and strange things happen: it joins all the sleep() times i.e. makes me wait for 2 seconds, and then prints the whole string Initializing. . . at once. Why does this happen? Is there a way to avoid that happening in the terminal? It works fine if I use the IDLE in both windows and linux.
This is because the output is being buffered.
You should add a sys.stdout.flush() after each write
It sounds like the difference is that stdout is automatically being flushed in IDLE. For efficiency, programming languages often save up a bunch of print calls before writing to the screen, which is a slow process.
Here's another question that has the answer you need:
How to flush output of Python print?