Goldy Tomy
Goldy Tomy

Reputation: 85

AttributeError: 'str' object has no attribute 'seek' using textfsm module (regex)

I'm trying to use the textfsm module which uses regex, and importing data into a text file. Below is my code:

from netmiko import ConnectHandler      
from textfsm import *  

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

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

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

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

data = re_table.ParseText(output)

print (output)
print(re_table.header)

for test in (re_table.header):
          fo.write(test)

fo.write("\n")

for row in data:
          for temp_row in data:
              fo.write(temp_row)
          fo.write("\n")


fo.close

But I'm getting this error:

Traceback (most recent call last): File "/Users/gtomy200/Desktop/Py/test.py", line 11, in re_table = TextFSM('xr_show_int_br','r') File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/textfsm.py", line 549, in init template.seek(0) AttributeError: 'str' object has no attribute 'seek'

Upvotes: 2

Views: 2088

Answers (2)

Kameron Ingrando
Kameron Ingrando

Reputation: 11

Came across the same error. Opening the file solved it for me.

with open('xr_show_int_br.txtfsm', 'r') as template:
    re_table = TextFSM(template)

Upvotes: 1

Dhruv Aggarwal
Dhruv Aggarwal

Reputation: 177

Seems like xr_show_int_br needs to be a file object. You are getting the error here

Upvotes: 0

Related Questions