0

I am using select.select() instead of input because I wanted a timeout for the input. I am using the "end" argument with the print() function because I want my terminal to have a line like this:

Type > TYPE SOMETHING HERE

Instead, I do not see "Type > " until after I type a string and press enter.

My code:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#Made by Devyn Collier Johnson, NCLA, Linux+, LPIC-1, DCTS
import sys, select

print('Type > ', end=" ")
INPUT, VOID0, VOID1 = select.select([sys.stdin], [], [], 3)

if (INPUT):
    print('You said, ' + sys.stdin.readline().strip())
else:
    print('You said nothing!')

I am using this script to test select.select() and print(str, end=" "). I read this post (How can I suppress the newline after a print statement?) and the official Python3 documentation for both commands.

Community
  • 1
  • 1
Devyn Collier Johnson
  • 4,124
  • 3
  • 18
  • 39

1 Answers1

1

stdout is buffered by default, to force it to display you need to flush it:

print('Type > ', end='')
sys.stdout.flush()

Note that print also supports this via keyword arguments:

print('Type > ', end='', flush=True)
Jon Clements
  • 138,671
  • 33
  • 247
  • 280
  • Thanks! That worked well. I cannot mark your answer until seven minutes has passed. I will keep this tab open on my browser so I can mark your answer. – Devyn Collier Johnson Nov 21 '13 at 14:53
  • @DevynCollierJohnson No worries - I take it, it's no coincidence I got an upvote for an answer with that select statement and then this question turns up :) – Jon Clements Nov 21 '13 at 14:55