python基础教程之文本菜单

发布时间:2020-01-23编辑:脚本学堂
本文介绍了python实现文本菜单的一例代码,python基础教程之文本菜单的实现方法,有需要的朋友参考下。

例子,python文本菜单。
 

复制代码 代码示例:

#!/usr/bin/evn python
# -*- coding: utf-8 -*-
#Edit: jb200.com
 
import os,sys
 
running = True
menu = """
             menu
------------------------------
    1:   Disk info
    2:   Mem info
    3:   Network info
    4:   Sys load info
    5:   Process info
    h:   Help
    q:   Quit
------------------------------
"""

menu_dict={
      "h": "echo help ^_^",
      "1": "df -h",
      "2": "free -m",
      "3": "netstat -lnt",
      "4": "uptime",
      "5": "ps x"
      }
 
def commands(args):
    cmd = menu_dict.get(args)
    return cmd
 
if __name__ == "__main__":
    os.system('clear')
    print menu  
    while running:
        cmd = raw_input("Input your commond :n")
        if cmd != 'q':
            os.system('clear')
            try:
                print menu
                if commands(cmd) != None:
                    fo = os.popen(commands(cmd))
                    print fo.read()
                else:
                    print "Input is Wrong!n"
            except Exception,e:
                print menu
                print e           
        else:
            print 'we will exit the menun'
            sys.exit()