AttributeError: 'CalledProcessError'对象没有'output'属性

1 投票
2 回答
4373 浏览
提问于 2025-04-17 09:46

...在Ubuntu 10.04上使用AutoKey 0.81.4

  1. 对Linux还比较陌生(不到一年)
  2. 这是我写的第一段Python代码

下面这个AutoKey的脚本总是出错,出现了以下错误。我到底哪里没理解呢??

Script name: 'find files script'
Traceback (most recent call last):
  File "/usr/lib/python2.6/dist-packages/autokey/service.py", line 442, in execute
    exec script.code in self.scope
  File "<string>", line 13, in <module>
AttributeError: 'CalledProcessError' object has no attribute 'output'

这个脚本

import time

time.sleep(0.10)
retCode, args =  dialog.input_dialog("Files to Find","enter a file name")
fmt = "find / -name \"{0}\" -type f -print 2>/dev/null "
if retCode == 0:
    if len(args) > 0:
        cmd = fmt.format(args)
        #dialog.info_dialog(title="the command",message=cmd)
        try:
            rc = system.exec_command(cmd, getOutput=True)
        except subprocess.CalledProcessError, e:
            dialog.info_dialog(title="the return",message=str(e.output))

2 个回答

-1

把 e.output 改成 e 就可以了。用 str(e) 可以得到错误信息的字符串。你可能想查一下异常(exceptions),看看它们支持哪些属性。我觉得 output 不是其中之一。

0

在Python 2.6之前,输出属性是不存在的。你可以使用subprocess.Popen和communicate()来实现这个功能。或者,你也可以按照这个链接的方法,把subprocess.check_output的功能移植到旧版本中(这个功能在2.6中也没有)。

撰写回答