The python implementation continues to execute the program by pressing any key, and python continues to execute

Source: Internet
Author: User

The python implementation continues to execute the program by pressing any key, and python continues to execute

When writing bat in windows, you can use the pause command to pause the program running. For example, a program that is frequently seen will prompt "press any key to continue..." on the terminal ......", After the user press enter on the terminal, the program can run. The purpose of this function is not mentioned yet, but I think many people may want to implement this function in python, in this way, when the python program is running, a prompt is suddenly given, and then a handsome carriage return is given. I think it must be a very professional feeling, at least you must be confused by yourself, so today we will learn this code. Here we define a function, so you can embed it into your program, you can call it wherever you want to call it. The Code is as follows:

#! /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 ...") Print '\ N'

The code is not explained much. Let's look at the comments. Here we will talk about the termios module. This module provides an interface to control the Io of the tty terminal. The first parameter of all its functions requires a file descriptor, it can be an integer file descriptor or a file object, because it can be controlled in the terminal display settings. Common scenarios are that users do not display the input password on the terminal, the Code is as follows:

def getpass(prompt=”Password: “):import termios, sysfd = sys.stdin.fileno()old = termios.tcgetattr(fd)new = termios.tcgetattr(fd)new[3] = new[3] & ~termios.ECHOtry:termios.tcsetattr(fd, termios.TCSADRAIN, new)passwd = raw_input(prompt)finally:termios.tcsetattr(fd, termios.TCSADRAIN, old)return passwdpasswd = getpass()print passwd

When running this script, you will be prompted to enter the password, and the entered password will be printed. Here are two examples to illustrate the simple usage of termios. You can run the program on your own.

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.