AttributeError:'tuple'对象在写入文件时没有属性'write'错误

时间:2017-06-23 17:12:58

标签: python python-3.x

from netmiko import ConnectHandler      
from textfsm import * 

cisco_device = { 'device_type' : 'cisco_ios', 'ip' : 'x.x.x.x', 'username':'gtomy200', 'password':'xxxxx'}
net_connect = ConnectHandler(**cisco_device)

fo=("testme.txt" , 'w')

output = net_connect.send_command("show int brief")

re_table = TextFSM(open('xr_show_int_br','r'))    

data = re_table.ParseText(output)

print (output)

for s in re_table.header:

          fo.write("%s;" %s)

fo.write("\n")

for row in data:
        print (row)
        for s in row:

                fo.write("%s" %s)
                fo.write("\n")

fo.close()

有人可以提供帮助,关于以下错误:

Traceback (most recent call last):
  File "/Users/gtomy200/Desktop/Py/test.py", line 20, in 
    fo.write("%s;" %s)
AttributeError: 'tuple' object has no attribute 'write'

2 个答案:

答案 0 :(得分:0)

您希望确保open文件:

fo = open("testme.txt" , 'w')
#    ^^^^

因为你正试图写一个两元组:

fo = ("testme.txt", 'w')
#    ^ no open

不会工作。

答案 1 :(得分:0)

fo元组,使用with open()进行文件操作。它更安全,更容易。

with open ("myfile.txt","w") as ff:
    ff.write("string") #you can't use anything but strings in here, 
                       #so convert your variables to string