Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 :)

like image 978
ggustafsson Avatar asked Feb 24 '11 06:02

ggustafsson


People also ask

How do you print a statement without using print function in Python?

How do you print without printing in Python? In Python 3, print() is a function that prints output on different lines, every time you use the function. However, you can avoid this by introducing the argument end and assigning an empty string to it.

Does Python print include newline?

In Python, the built-in print function is used to print content to the standard output, which is usually the console. By default, the print function adds a newline character at the end of the printed content, so the next output by the program occurs on the next line.

Can you print in Python without newline?

Printing without a new line is simple in Python 3. In order to print without newline in Python, you need to add an extra argument to your print function that will tell the program that you don't want your next string to be on a new line. Here's an example: print("Hello there!", end = '') print("It is a great day.")

How do you enter without a new line in Python?

In short: You can't. raw_input() will always echo the text entered by the user, including the trailing newline. That means whatever the user is typing will be printed to standard output. If you want to prevent this, you will have to use a terminal control library such as the curses module.


2 Answers

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

like image 50
ide Avatar answered Oct 19 '22 06:10

ide


Python is buffering the output until a newline (or until a certain size), meaning it won't print anything until the buffer gets a newline character \n. This is because printing is really costly in terms of performance, so it's better to fill a small buffer and only print once in a while instead.

If you want it to print immediately, you need to flush it manually. This can be done by setting the flush keyword argument to True.

import time

word = "One letter at a time"

for letter in word:
    print(letter, end='', flush=True)
    time.sleep(0.25)
like image 34
Ted Klein Bergman Avatar answered Oct 19 '22 04:10

Ted Klein Bergman