1、首先,按回车键退出。#coding=utf-8raw_input(unicode('按回车键退出...','utf-8').encode('gbk'))
2、按逻晶诚湮任意键继续。import osos.system('pause')接下来即Linux下实现python版本的按任意键退出。初学Python时在总想实现一个按任意键继续/退出的程序(受.bat毒害), 奈何一直写不出来, 最近学习Unix C时发现可以通过 termios.h 库来实现, 尝试一下发现Python也有这个库, 所以终于写出一个这样的程序. 下面是代码:#!/usr/bin/env python# -*- coding:utf-8 -*-import osimport sysimport termiosdef press_any_key_exit(msg):
3、# 获取标准输入的描述符fd = sys.stdin.fileno()# 获取标准输入(终端)的设置old_ttyinfo = termios.tcgetattr烫喇霰嘴(fd)# 配置终端new_ttyinfo = old_ttyinfo[:]# 使用非规范模式(索引3是c_lflag 也就是本地模式)new_ttyinfo[3] &= ~termios.ICANON# 关闭回显(输入不会被显示)new_ttyinfo[3] &= ~termios.ECHO
4、# 输出信息sys.stdout.write(msg)sys.stdout.flush()# 使设置生效termios.tcsetattr(fd, termios.TCSANOW, new_ttyinfo)# 从终端读取os.read(fd, 7)
5、# 还原终端设置termios.tcsetattr(fd, termios.TCSANOW, old_ttyinfo)if __name__ == "__main__":press_any_key_exit("按任意键继续...")press_any_key_exit("按任意键退出...")