userPassingBy
userPassingBy

Reputation: 339

Python print on same line after sleep

I checked Print new output on same line

but am still confused:

import time
print("HI ", end=""),
time.sleep(2)
print("BYE")

it does print HI BYE on the same line, but it prints all at once after the sleep. I want to print HI then sleep for 2 seconds and print BYE after the 2s.

Ideas?

Thanks.

Upvotes: 2

Views: 1900

Answers (1)

Andrey Shokhin
Andrey Shokhin

Reputation: 12182

You can flush your output:

impost sys
import time
print("HI ", end="")
sys.stdout.flush()
time.sleep(2)
print('BYE')

If you write to file-like object, you need flush it:

file.flush()

in python 3.3 you can just add key-word parameter to print function:

print(smth, flush=True)

Upvotes: 5

Related Questions