簡體   English   中英

AttributeError:'tuple'對象在寫入文件時沒有屬性'write'錯誤

[英]AttributeError: 'tuple' object has no attribute 'write' error while writing into a file

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'

您想確保open文件:

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

因為它是你試圖寫一個兩元組:

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

哪個不行。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM