例子,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()