我正在尝试使用python的cmd模块,我在一个网站上得到了这个测试代码 在Python3.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模块的错误。在
^{pr2}$在4号线。在
我试过你用
python3.2.3
编写脚本,它很管用。您是否有一个名为cmd.py
的文件与此文件所在的目录相同?如果这样做,则import cmd
将不会导入正确的模块。相反,它将导入当前目录中的cmd.py
。在我在目录中创建了一个名为
cmd.py
的文件,并在运行脚本时遇到了与您相同的错误。所以删除当前目录中的cmd.py
,或者如果你有它的话,给它起个别的名字。在相关问题 更多 >
编程相关推荐