我正在尝试使用 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()
Run Code Online (Sandbox Code Playgroud)
我收到 cmd 模块的此错误。
AttributeError: 'module' object has no attribute 'Cmd' …Run Code Online (Sandbox Code Playgroud)