Python Concepts/Console Input

Lesson
In the context of this lesson, the word "console" implies the visual display which you see in front of you when you sit at your desk, and also the attached keyboard. The keyboard provides "console input" while the visual display provides console output.

Other input devices such as mice, joysticks, game controllers and touch screens are not covered in this lesson.

Python reserves one file object for console input, standard input: The above file object is always open for reading in text mode. It has its associated file descriptor:

More information about sys.stdin:

Because  is a file object, it responds to methods applicable to file objects. However it is recommended that you do not attempt such methods or operations as these on

Reading from stdin
File descriptor 0 usually means standard input. When your python script reads from standard input, this usually means that it is reading data which you are typing on your keyboard. Because file descriptor 0 reads text, the new line character at the end of each line becomes significant.

In the real world your python script will probably read from stdin after supplying a prompt for your response. $ python3.6 t4.py Enter date-of-birth [mm/dd/yyyy]: 12/31/1967 You entered 12/31/1967 $ python's built-in function  simplifies prompting and reading from stdin:

$ python3.6 t4.py Enter date-of-birth [mm/dd/yyyy]: 12/31/1967 You entered 12/31/1967 $ If the readline module was loaded, then input will use it to provide elaborate line editing and history features similar to command line editing with emacs.

Detecting timeout on stdin
The python script above sends a prompt to stdout and expects a response on stdin. What should happen if the person at the keyboard is called away and no response is forthcoming?

You decide to terminate the session after 30 seconds of inactivity. Here is one way to do it:

$ cat t6.py $ python3.6 t6.py ; echo $? Enter date-of-birth [mm/dd/yyyy]: 12/31/1999 # Normal input. You entered 12/31/1999 0 # Normal exit 0. $ $ python3.6 t6.py ; echo $? Enter date-of-birth [mm/dd/yyyy]: # No input. Operation timed out. SystemExit detected. 97 # Exit with error status 97. $ $ python3.6 t6.py ; echo $? Enter date-of-birth [mm/dd/yyyy]: # Entered ^C. KeyboardInterrupt detected. 98 # Exit with error status 98. $

Input stream redirected
Usually  is connected to the keyboard so that a command like reads data from the keyboard until end-of-line is detected. However, input can come from a pipe.

$ cat t4.py $ python3.6 t4.py sys.stdin.isatty: True                    # This is interactive session. Enter date-of-birth [mm/dd/yyyy]: 12/31/1967 # Input from keyboard. You entered 12/31/1967 $ $ echo '12/31/1988' | python3.6 t4.py # Input from a pipe. sys.stdin.isatty: False            # Not interactive. Enter date-of-birth [mm/dd/yyyy]:    # No input from keyboard. You entered 12/31/1988 $

Console input from terminal
The terminal can provide console input. On Unix the device name of the terminal is available and it behaves like a file: $ cat t5.py

$ python3.6 t5.py Name of my terminal is: /dev/ttys003 File object opened for reading from my terminal is: <_io.TextIOWrapper name='/dev/ttys003' mode='r' encoding='UTF-8'> Enter name of your favorite pet: Fido # Enter 'Fido\n' then ^D for end-of-file. Input from terminal was: Fido len(pet) = 5 # String pet contains 5 characters (including the new-line) and without the ^D. $

Assignments



 * How do you know if  contains data waiting to be read?


 * If  contains unwanted data, how do you clear the buffer?

Further Reading or Review

 * Previous Lesson: Console Output
 * This Lesson: Console Input
 * Next Lesson: Files
 * Course Home Page