【发布时间】:2014-03-24 02:45:29
【问题描述】:
我正在尝试使用 python 的 cmd 模块,我在一个网站上得到了这个测试代码 在 python 3.2 中编译此代码时
import cmd
import os
class ShellEnabled(cmd.Cmd):
last_output = ''
def do_shell(self, line):
"Run a shell command"
print ("running shell command:", line)
output = os.popen(line).read()
print (output)
self.last_output = output
def do_echo(self, line):
"Print the input, replacing '$out' with the output of the last shell command"
# Obviously not robust
print (line.replace('$out', self.last_output))
def do_EOF(self, line):
return True
if __name__ == '__main__':
ShellEnabled().cmdloop()
我收到 cmd 模块的此错误。
AttributeError: 'module' object has no attribute 'Cmd'
在第 4 行。
【问题讨论】:
-
你好,我试过你的代码,没有发现错误...顺便问一下,你调用的函数 cmdloop() 在哪里?
-
@lowitty 是 cmd.Cmd 的方法。在 python 3.3 上工作正常。
-
@m.wasowski 谢谢!其实我试过这个脚本并得到输出:(Cmd)并且没有错误。
-
所以它不能在 python 3.2 上运行?
标签: python cmd attributeerror