I know how to utilize time.sleep(), but I was curious how to print something like so:
"hey...(pause)...you...(pause)....there"
where the 'pause' is some time.sleep() interval. I can only print these intervals on separate lines. Is there a way to keep it all on one line?
In python 2:
print "hey...",
time.sleep(0.5)
In python 3:
print("hey...", end=' ')
time.sleep(0.5)
In a print statement in python2.x, you can suppress the newline with a trailing comma:
print "hey...",
time.sleep(1)
print "...you...",
time.sleep(1)
print "....there"
On python3.x (or with from __future__ import print_function enabled), you use the end keyword to the print function:
print("hey...", end="")
etc.
Or, finally, you can always1 write to the sys.stdout stream:
import sys
sys.stdout.write("hey...")
time.sleep(1)
...
The advantage here is that you can flush it explicitly:
sys.stdout.flush()
1technically not always. sys.stdout could be replaced by something else: sys.stdout = 3 -- But making a mess without cleaning up after yourself is terribly rude. However, if you ever find yourself in that situation, sys.__stdout__ is a backup ;-).
Related
[printing slowly (Simulate typing)
I got my answer from the link above but it only works when you put the string as a parameter when calling function.
I want the code to print slowly every time when I use print().
is it possible?
Yes, you can do it like this, however, I think it's not a good idea:
import time
def dprint(string):
for letter in string:
__builtins__.print(letter,end = '', flush=True)
time.sleep(.1)
__builtins__.print("")
print = dprint
print("something")
Yes, you can do it using the stdout version as below.
import sys, time
def print(s):
for letter in s:
sys.stdout.write(letter)
time.sleep(.1)
print("Foo")
Changing the default behaviour of print() is not recommended and was only introduced for purpose of porting Python 2 programs easily. Moreover overloading the print function without a special parameter will make the default functionality of print() moot.
Create another function with adds a delay to the prints. Also remember that you cannot use print() because it appends a new line. You’ll have to you sys.stdout.write()
So a basic function would look like:
def typrint(x):
for i in len(x):
sys.stdout.write(x[i])
sleep(0.05)
sys.stdout.write(“\n”)
Check this article to see why Python updated print() to a function
I am using this as the solution for the problem,
import sys,time
def delay(str):
for i in str:
sys.stdout.write(i)
sys.stdout.flush()
time.sleep(0.04)
Note: You need to add in every print statement or here "delay" statement "\n".
I have created a simple python program that will count up to 10
What I am trying to achieve is for the program to delete the previous number and print the new number
This is the code that I have created:
import sys
import time
for i in range(10):
text = "\r" + str(i)
sys.stdout.write(text)
sys.stdout.flush()
time.sleep(1)
Which outputs:
0123456789
Changing the code to have the "\r" after the str(i) didn't work either
import sys
import time
for i in range(10):
text = str(i) + "\r"
sys.stdout.write(text)
sys.stdout.flush()
time.sleep(1)
Which also resulted in:
0123456789
I was looking for it to count up, and display at the end only the 9 however it doesn't overwrite the previous numbers
Edit:
I am using Windows, Python3
Edit 2:
How to overwrite the previous print to stdout in python? Does not give me a working answer, it still doesn't do what I want
Therefore due to my excellent reasoning it is not a duplicate :P
As an alternative, you can clear the whole window, by using os.system("cls")
There are some control symbols accepted by virtual terminals. One of them is '\b' that moves a carret for one place back. This one is accepted on Windows too and I will use it in my example below. Unix terminals accept a lot more of controls including color changes and more.
from time import sleep
import sys, os
def clear ():
os.system("cls" if sys.platform.startswith("win") else "clear")
clear()
s = "1"
sys.stdout.write(s)
for x in range(2, 21):
sleep(1)
# Return carret to beginning of line:
l = len(s)
s = l*"\b"
sys.stdout.write(s)
# Clear line (just in case):
s = l*" "
sys.stdout.write(s)
# Return to the beginning again:
s = l*"\b"
sys.stdout.write(s)
# Write over new text:
s = str(x)
sys.stdout.write(s)
Use the end parameter to specify a "\r" as the line ending.
import time
for i in range(10):
print(i, end="\r")
time.sleep(1)
I would do it like this:
for x in range(10):
print("{}".format(x), end="\r")
I believe your code should work perfectly fine. The problem should be that you are using a Windows terminal. Try using Linux. Most code does not work in Windows. You can refer this link to know why you have to stop using python on Windows.
This is also an alternate code you can try.
import time
for i in range(10):
text = str(i)
print(text,end = "\r")
time.sleep(1)
Why not use the standard print of python? Althougth given in many debugger screens this tends to not work. In terminal (ubuntu) it does
import time
for i in range (10):
print(i, end='\r')
time.sleep(1)
Probably for stdout something exists too but in this case I usually use the print of python3
This question already has an answer here:
Why doesn't print output show up immediately in the terminal when there is no newline at the end?
(1 answer)
Closed last month.
I was trying to create a progress-like thing by printing a dot every second on the same line. Something like "Fishing.....". This is what I used:
import time
print('Fishing', end='')
for i in range(5):
time.sleep(1)
print('.', end='')
But it waits for 5 seconds and prints Fishing..... all at once. But when I don't use the end='', it prints dots every second, but on separate lines like so
Fishing.
.
.
.
.
My questions:
Why does print behave this way?
How can I print a dot every second, but on the same line?
Why does print behave this way?
This has less to do with print and more with your terminal. For performance reasons, the text only gets "flushed" everytime there's a newline character, and not one character at a time.
How can I print a dot every second, but on the same line?
By "flushing" the standard output manually everytime you printed something:
import time
import sys
print('Fishing', end='')
sys.stdout.flush()
for i in range(5):
time.sleep(1)
print('.', end='', flush=True) # another way
If you need this all the time, you could define a seperate flushing print function:
from functools import partial
myprint = partial(print, end='', flush=True)
myprint('Fishing')
for i in range(5):
time.sleep(1)
myprint('.')
This is because print is considered to be an expensive operation: it will usually wait until it has to print a lot of data, or until a new line is encountered (usually only if output is written to a terminal):
Output buffering is determined by file. Use file.flush() to ensure, for instance, immediate appearance on a screen.
Evidently it is not expensive in the sense that it takes minutes: but asking the terminal operator to print new content, or the operating system to write data to a file (in case of I/O redirection) is still not "lightning fast".
You can force to print all data that is still in the queue, by using a flush on the standard output channel.
use
print('.', end="", flush=True)
Im new to python and was wondering how to make a loading animation while my program runs. I need this because I don't want users thinking that the program is caught in a dead loop. I prefer a something like...
Loading...(with the dots disappearing and reappearing one by one)
Thanks!
If your output window supports the carriage return character, you can print it to make the cursor return to the beginning of the current line (provided you end your print statement with a comma, so a newline character isn't automatically printed). Then subsequent prints will overwrite what was already printed. You can use this to do very simple one line animation. Example:
import time
print "Starting program."
print "Loading ",
time.sleep(1) #do some work here...
print "\rLoading. ",
time.sleep(1) #do some more work here...
print "\rLoading.. ",
time.sleep(1) #do even more work...
print "\rLoading...",
time.sleep(1) #gratuitious amounts of work...
print "\rLoading ",
... Where time.sleep(1) is a placeholder representing the actual work you want to do.
Result:
Starting program.
Loading
Then, one second later:
Starting program.
Loading.
Then, one second later:
Starting program.
Loading..
Then, one second later:
Starting program.
Loading...
Then, one second later:
Starting program.
Loading
etc.
Compatibility note: in 3.X, print is no longer a statement, and the "end with a comma" trick no longer works. Instead, specify the end parameter:
print("\rLoading...", end="")
The most proper way I can think of to do it would be using threading.
You would initiate a thread that starts displaying some indication that the program is doing something and then open a new thread that actually does the work.
When the thread doing the work finished then you can move on with whatever else the program does.
This looks ok when ran in windows command prompt, not sure how linux will like it:
import threading
import time
import os
import queue
q = queue.Queue()
q.put(False)
class counter(object):
def __init__(self):
wait_label = "Loading"
self.stop_flag = q.get()
while not self.stop_flag:
try:
self.stop_flag = q.get_nowait()
except:
pass
os.system('cls') # might need to change this command for linux
wait_label += "."
print(wait_label)
time.sleep(1)
class other(counter):
def __init__(self):
time.sleep(15)
q.put(True)
counter_thread = threading.Thread(None, counter)
counter_thread.start()
other_thread = threading.Thread(None, other)
other_thread.start()
To reduce the code length, we can loop it.
import time
# flag variable to print the dots and it's value increases inside the while loop.
flag = 1
# To print the dots we use while loop. In total, 4 dots will be printed.
while flag < 5:
print("\rLoading, Please Wait " + ("." * flag), end=" ")
time.sleep(1)
flag = flag + 1
I am making a program that has a "loading bar" but I can't figure out how to make the code shorter. This might be a simple fix for all I know, but for the life of me, I just can't figure it out. Here is what I have tried to do so far:
def ldbar():
print "Load: 1%"
time.sleep(0.5)
os.system('clear')
print "Load: 2%"
time.sleep(0.5)
os.system('clear')
print "Load: 3%"
time.sleep(0.5)
os.system('clear')
print "Load: 4%"
time.sleep(0.5)
os.system('clear')
print "Load: 5%"
#So on and so forth up to 100%
ldbar()
So, like I said, is there anyway I can make this shorter?
This should work:
def ldbar():
for i in range(1, 100):
print "Load: {}%\r".format(i),
sys.stdout.flush()
time.sleep(0.5)
ldbar()
It uses a for loop to avoid having the same code over and over again. In the print statement I use \r which moves the cursor to the front of the line, allowing it to be overwriten which is why sys.stdout.flush is used to make sure the output is printed without a newline (notice the comma at the end of the print statement which says that a newline should not be printed).
For Python 3 you would use this (but I think you're using python 2):
def ldbar():
for i in range(1, 100):
print("Load: {}%\r".format(i), end="")
sys.stdout.flush()
time.sleep(0.5)
ldbar()
Here's a nice version using a context manager:
from contextlib import contextmanager
import sys
#contextmanager
def scoped_progress_bar(num_steps, message):
class Stepper(object):
'''
Helper class that does the work of actually advancing the progress bar message
'''
def __init__(self, num_steps, message):
self.current_step = 0.0
self.num_steps = num_steps
self.message = message
def step(self, steps = 1.0):
self.current_step += steps
sys.stdout.write('\r{}:{}%'.format(message, (self.current_step/self.num_steps)*100))
sys.stdout.flush()
stepper = Stepper(num_steps, message) # This is where we actually create the progress bar
yield stepper.step # This is where we do the yield secret sauce to let the user step the bar.
# Finally when the context block exits we wind up back here, and advance the bar to 100% if we need to
if stepper.current_step < stepper.num_steps:
stepper.step(stepper.num_steps - stepper.current_step)
The advantage of this method is that
You can specify an arbitrary number of steps
You can step an arbitrary number of steps
Even if you don't hit the end of the number of steps, the context manager will always print 100% at the end
You can specify an arbitrary message
Usage:
with scoped_progress_bar(10, 'loading') as step:
for i in xrange(7):
step()
time.sleep(0.5)
Which prints:
loading: 10%
loading: 20%
...
loading: 70%
loading: 100%
It's likely a bit overkill for your situation, but thought I'd provide it just in case.
An important thing to note with all of these answers is that they assume you won't be printing out stuff during the process in which you're advancing the progress bar. Doing so will still work just fine, it just might not look like what you expect.
First off, use the Progressbar module (https://pypi.python.org/pypi/progressbar), it already does everything you'll ever want from a text-mode progress bar, and then some.
Now for a fix for your specific implementation, what you want to do is write a bar to stdout (or stderr) with no line return, then erase it, then draw it again. You do it like so:
import sys
import time
sys.stdout.write("0%")
# stdout is line-buffered and you didn't print a newline,
# so nothing will show up unless you explicitly call flush.
sys.stdout.flush()
time.sleep(2)
# Move the cursor back to the beginning of the line
# and overwrite the characters.
sys.stdout.write("\r1%")
sys.stdout.flush()
time.sleep(2)
sys.stdout.write("\r2%")
sys.stdout.flush()
# etc.
But really, use progressbar.