AttributeError:“列表”对象没有属性“ rfind”

时间:2019-03-02 19:00:40

标签: python bash

这是我的代码:

with open (basefile,"r") as file:

    for servers in file.read().splitlines():

      rsltfile = servers+"_SNMPCHECK_RSLT.txt"

      command = (["snmp-check","-c"],["community.txt"],servers,[">>"],rsltfile)

      with open(rsltfile,"w") as rslt:

        subprocess.call(command)

这是它的回溯:

Traceback (most recent call last):
AttributeError: 'list' object has no attribute 'rfind'

我无法在“追溯”中粘贴每一行,因为它不断给我带来错误。但是无论如何,我似乎无法使用call方法,但是却收到此错误,而且我不知道发生了什么。

1 个答案:

答案 0 :(得分:1)

  1. 不要使用嵌套列表/元组。
  2. >>是一个shell功能,在我的测试中,即使使用shell = True,该功能似乎也不起作用。如果您的命令是字符串而不是列表,则可能会起作用,但这也不理想。
command = ["snmp-check","-c","community.txt",servers]
with open(rsltfile,"a") as rslt:  # note the "a"
    subprocess.call(command, stdout=rslt)