我正在尝试使用模块 python cmd 编写一个 python CLI 程序。当我尝试在我的 CLI 程序中执行另一个 python 脚本时,我的目标是在其他文件夹中有一些 python 脚本,在其他文件夹中有 CLI 程序。我正在尝试使用 CLI 程序执行那些 python 脚本。
下面是用于执行其他脚本的 os.popen 方法有 CLI 程序:
import cmd
import os
import sys
class demo(cmd.Cmd):
def do_shell(self,line,args):
"""hare is function to execute the other script"""
output = os.popen('xterm -hold -e python %s' % args).read()
output(sys.argv[1])
def do_quit(self,line):
return True
if __name__ == '__main__':
demo().cmdloop()
野兔是错误的:
(Cmd) shell demo-test.py
Traceback (most recent call last):
File "bemo.py", line 18, in <module>
demo().cmdloop()
File "/usr/lib/python2.7/cmd.py", line 142, in cmdloop
stop = self.onecmd(line)
File "/usr/lib/python2.7/cmd.py", line 221, in onecmd
return func(arg)
TypeError: do_shell() takes exactly 3 arguments (2 given)
有一些到其他 cmd CLI 程序的链接 1 = cmd – 创建面向行的命令处理器 2 =使用 Cmd 对象构建的控制台(Python 配方)
请在您的系统中运行上述代码。