experts, the problem is a common and I have tried based on the solutions and examples in SO but still have problem to write all of my devices print output into a file. I have test using several methods and most of the time I'm only got last line of output in my text file. The problem below almost same as mine...but no luck for me.
How to delete the contents of a file before writing into it in a python script?
I test the script to run specific command based on the type of device. If devices A run command A, if devices B run command B, if device C run command c. List of devices in text file ( the list have type and ip address). When i use 'w' the file content only the last line but when I use 'a' it will save all the content of all the devices in the list but when I run again the script it will continue to write on the last pointer and thus I got duplicate content..it append and keep append.
when use outFileName, "w" output content only take last line content
OUTPUT CONTENT for device type C IP 10.2.10.12
when use outFileName, "a" output content first time run the script as follows
OUTPUT CONTENT for device type A IP 192.168.100.100
OUTPUT CONTENT for device type A IP 192.168.100.110
OUTPUT CONTENT for device type B IP 10.1.10.100
OUTPUT CONTENT for device type C IP 10.2.10.10
OUTPUT CONTENT for device type C IP 10.2.10.11
OUTPUT CONTENT for device type C IP 10.2.10.12
when run script second time ...the file contains duplicates as follows
OUTPUT CONTENT for device type A IP 192.168.100.100
OUTPUT CONTENT for device type A IP 192.168.100.110
OUTPUT CONTENT for device type B IP 10.1.10.100
OUTPUT CONTENT for device type C IP 10.2.10.10
OUTPUT CONTENT for device type C IP 10.2.10.11
OUTPUT CONTENT for device type C IP 10.2.10.12
OUTPUT CONTENT for device type A IP 192.168.100.100
OUTPUT CONTENT for device type A IP 192.168.100.110
OUTPUT CONTENT for device type B IP 10.1.10.100
OUTPUT CONTENT for device type C IP 10.2.10.10
OUTPUT CONTENT for device type C IP 10.2.10.11
OUTPUT CONTENT for device type C IP 10.2.10.12
The script as follows
#Define functions for each device_type
def type_A(ip):
return{
'device_type': 'A',
'ip': ip,
'username': 'usr10',
'password': 'password',
}
def type_B(ip):
return{
'device_type': 'B',
'ip': ip,
'username': 'usr10',
'password': 'password',
}
def type_C(ip):
return{
'device_type': 'C',
'ip': ip,
'username': 'usr10',
'password': 'password',
}
#Function to create output text file
def writeOutFile(outFileName, string):
with open(outFileName, "w") as f:
outfile = f.write(string)
#Open Text file that contain device type and ip address
deviceFile = open('devices.txt','r')
#Create list llist for each line in the file.
#The first item is the device type,
#The second item is the IP address
for line in deviceFile:
llist = line.split()
ipAd = llist[1]
#Check the first item of the list to determine device type and set
#variables
if llist[0] == 'A':
dvs = type_A(ipAd)
sendCommand = 'command for device type A'
elif llist[0] == 'B':
dvs = type_B(ipAd)
sendCommand = 'command for device type B'
elif llist[0] == 'C':
dvs = type_C(ipAd)
sendCommand = 'command for device type c'
else:
print("no valid device type")
break
dvs_connect = ConnectHandler(**dvs)
sendCommand = (dvs_connect.send_command(sendCommand))
#This print all the devices output on the terminal
print(sendCommand)
#Generate output file
outFileName = "outputFile.txt"
#function call to write the output string into a text file
writeOutFile(outFileName, sendCommand)
dvs_connect.disconnect()
deviceFile.close()
devices.txt list
A 192.168.100.100
A 192.168.100.110
B 10.1.10.100
C 10.2.10.10
C 10.2.10.11
C 10.2.10.12
outputFile.txt The file output just content the last line content ...the output content of A and B seems already overwrite.
OUTPUT CONTENT for device type C IP 10.2.10.12
I expect how many time i run the script it will overwrite the existing content of text file but no duplicate...if I got 6 devices in my devices.txt list...meaning that I should have 6 device output in my text file output. The output file should be like below
OUTPUT CONTENT for device type A IP 192.168.100.100
OUTPUT CONTENT for device type A IP 192.168.100.110
OUTPUT CONTENT for device type B IP 10.1.10.100
OUTPUT CONTENT for device type C IP 10.2.10.10
OUTPUT CONTENT for device type C IP 10.2.10.11
OUTPUT CONTENT for device type C IP 10.2.10.12
I really hope someone could help me.. please assist. Thank you.