To enable the python version, press any key to continue/exit and any key to exit python.

Source: Internet
Author: User

To enable the python version, press any key to continue/exit and any key to exit python.

Some people in the group asked me one day that I had to input or raw_input in python and then press enter to get the input value. How can I implement the function of suspending or exiting any key, I didn't think much at the time, because it was not long to come into contact with python, mainly in Linux.

To implement this function, you need to pause the program, wait and capture a user's keyboard input, and then continue to execute. Python has a built-in library to help us implement this function, but we need to treat Windows and Linux differently.

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.

1. Press enter to exit.

# Coding = utf-8raw_input (unicode ('Press the Enter key to exit... ', 'utf-8'). encode ('gbk '))

2. Press any key to continue.

import osos.system('pause')

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 <Unix-Linux Programming Practice tutorial>)

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.

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.