简体   繁体   中英

Python3: Using “select.select” with print(str, end=' ')

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM