属性错误Cmd modu

2024-09-30 02:33:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用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号线。在


Tags: 模块theimportselfcmdoutputosdef
1条回答
网友
1楼 · 发布于 2024-09-30 02:33:12

我试过你用python3.2.3编写脚本,它很管用。您是否有一个名为cmd.py的文件与此文件所在的目录相同?如果这样做,则import cmd将不会导入正确的模块。相反,它将导入当前目录中的cmd.py。在

我在目录中创建了一个名为cmd.py的文件,并在运行脚本时遇到了与您相同的错误。所以删除当前目录中的cmd.py,或者如果你有它的话,给它起个别的名字。在

相关问题 更多 >

    热门问题