Print without newline under function doesn't work as it should in Python

I'm doing a progressbar for my uploading script and therefor i want to print a row of multiple '#' but i can't get it to work. When i tell Python to not add newline it does remove it but it doesn't work as expected under functions. Using "print ('#', end='')" in Python 3 or "print '#'," in Python 2 removes it but when executed under a functions it doesn't print anything out until the whole function is finished, it should not wait just like normal print.

import time

i = 0

def status():
    print('#', end='')

while i < 60:
    status()
    time.sleep(1)
    i += 1

This should print '#' every second but it doesn't. It prints them all after 60 seconds. Using just print('#') prints it out every second as expected. I really need a fix for this. Please help!

Solution: "sys.stdout.flush()" after each print :)


Solution 1:

You probably need to flush the output buffer after each print invocation. See How to flush output of Python print?