专家,这个问题很常见,我已经尝试过基于 SO 中的解决方案和示例,但仍然无法将我所有的设备打印输出写入文件。我使用几种方法进行了测试,大多数时候我的文本文件中只有最后一行输出。下面的问题与我的几乎相同......但我没有运气。
我测试脚本以根据设备类型运行特定命令。如果设备 A 运行命令 A,如果设备 B 运行命令 B,如果设备 C 运行命令 c。文本文件中的设备列表(列表有类型和 IP 地址)。当我使用“w”时,文件内容仅在最后一行,但当我使用“a”时,它将保存列表中所有设备的所有内容,但当我再次运行脚本时,它将继续写入最后一个指针和因此我得到了重复的内容..它追加并保持追加。
使用 outFileName 时,"w" 输出内容只取最后一行内容
OUTPUT CONTENT for device type C IP 10.2.10.12
使用 outFileName 时,“a”输出内容第一次运行脚本如下
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
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
脚本如下
#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 列表
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 文件输出只是内容最后一行内容...A 和B 的输出内容似乎已经被覆盖。
OUTPUT CONTENT for device type C IP 10.2.10.12
我希望我运行脚本多少次会覆盖文本文件的现有内容但没有重复...如果我的 devices.txt 列表中有 6 个设备...这意味着我的文本文件中应该有 6 个设备输出输出。输出文件应如下所示
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
我真的希望有人可以帮助我..请协助。谢谢你。