几分钟内用Python做个简化版的CMD
2009 3 9 11:32 AM 3099次查看
			
			# -*- coding: gbk -*-
import cmd
import os
class MyCmd(cmd.Cmd):
  def __init__(self):
    cmd.Cmd.__init__(self)
    self.prompt = '> '        # 定义命令行提示符
  def do_dir(self, arg):      # 定义dir命令所执行的操作
    if not arg:
      self.help_dir()
    elif os.path.exists(arg):
      print '\n'.join(os.listdir(arg))
    else:
      print 'No such path exists.'
  def help_dir(self):         # 定义dir命令的帮助输出
    print "syntax: dir path -- Displays a list of files and subdirectories in a directory."
  def do_quit(self, arg):     # 定义quit命令所执行的操作
    exit()
  def help_quit(self):        # 定义quit命令的帮助输出
    print "syntax: quit -- terminates the application."
  # 定义quit的快捷方式
  do_q = do_quit
# 创建MyCmd实例并运行
MyCmd().cmdloop()Python真是方便啊,不知道为什么学校只教C/C++/Java~补充:还有个CommandLineApp模块,感兴趣的可以看看
http://www.doughellmann.com/articles/CommandLineApp/index.html
0条评论 你不来一发么↓