How to exit Python by pressing any key in Linux

Source: Internet
Author: User
This article mainly introduces how to implement exit by any key in Python in Linux. This article is very detailed and has reference value, if you need a friend, you can refer to a student in the group one day and ask, in python, I have to input or raw_input, and then press Enter to get the input value, I didn't think much about how to enable any key to exit the pause function at the time, because it was not too long to access python, mainly in Linux.

Of course, Windows will be a little simpler. if you have installed a python environment in Windows, the default built-in module is msvcrt, import msvcrt, and then call msvcrt. getch. In Linux, press any key to exit the python version.

When you are a beginner in Python, you always want to implement a program to continue/exit by pressing any key (subject. bat poison), but I can't write it out. recently I learned about Unix C and found that it can use termios. h library to achieve, try to find that Python also has this library, so finally write such a program. the following code is used:

#! /Usr/bin/env python #-*-coding: UTF-8-*-import osimport sysimport termiosdef press_any_key_exit (msg): # get standard input descriptor fd = sys. stdin. fileno () # obtain the standard input (terminal) setting old_ttyinfo = termios. tcgetattr (fd) # configuration terminal new_ttyinfo = old_ttyinfo [:] # use non-standard mode (index 3 is c_lflag or local mode) new_ttyinfo [3] & = ~ Termios. ICANON # disable echo (the input is not displayed) new_ttyinfo [3] & = ~ Termios. ECHO # output information sys. stdout. write (msg) sys. stdout. flush () # enable the setting to take effect. tcsetattr (fd, termios. TCSANOW, new_ttyinfo) # read OS from the terminal. read (fd, 7) # Restore the terminal to set termios. tcsetattr (fd, termios. TCSANOW, old_ttyinfo) if _ name _ = "_ main _": press_any_key_exit ("press any key to continue... ") press_any_key_exit (" press any key to exit... ")

For more information about termios, refer to the Linux manual:

man 3 termios

I also want to add three modes of * nix terminal (from )

Standard mode

The standard mode has also become the cooked mode, which is a common mode for users. the characters entered by the driver are stored in the buffer and sent to the program only when the enter key is received. buffer data so that the driver can implement the most basic editing functions. the specific keys assigned to these functions can be set in the driver. you can use the stty command or the system to call tcsetattr to modify the settings.

Non-standard mode

When the buffer and edit functions are disabled, the connection becomes invalid. the terminal processor still processes specific characters, such as converting Ctrl-C and line breaks, but the edit key is meaningless, therefore, the corresponding input is regarded as a regular data input program and needs to be edited by itself.

Raw mode

When all processing is disabled, the driver passes the input directly to the program, and the connection is in raw mode.

The above section describes how to implement Python exit by pressing any key in Linux. I hope it will be helpful to you. if you have any questions, please leave a message, the editor will reply to you in a timely manner. I would like to thank you for your support of PHP Chinese network!

For more articles about how to exit Python by pressing any key in Linux, refer to PHP!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.