Python 删除现有文件的内容并重新写入内容
专家们,这是一个常见的问题,我尝试了基于SO中的解决方案和示例,但仍然存在将所有设备的打印输出写入文件的问题。我使用了几种方法进行测试,大多数时候我只得到文本文件中的最后一行输出。 下面的问题和我的差不多,但我运气不好 我测试脚本以根据设备类型运行特定命令。如果设备A运行命令A,如果设备B运行命令B,如果设备C运行命令C。文本文件中的设备列表(该列表具有类型和ip地址)。当我使用“w”时,文件内容仅在最后一行,但当我使用“a”时,它将保存列表中所有设备的所有内容,但当我再次运行脚本时,它将继续在最后一个指针上写入,因此我得到了重复的内容..它追加并保持追加 当使用outFileName时,“w” 输出内容仅取最后一行内容Python 删除现有文件的内容并重新写入内容,python,overwrite,file-writing,erase,Python,Overwrite,File Writing,Erase,专家们,这是一个常见的问题,我尝试了基于SO中的解决方案和示例,但仍然存在将所有设备的打印输出写入文件的问题。我使用了几种方法进行测试,大多数时候我只得到文本文件中的最后一行输出。 下面的问题和我的差不多,但我运气不好 我测试脚本以根据设备类型运行特定命令。如果设备A运行命令A,如果设备B运行命令B,如果设备C运行命令C。文本文件中的设备列表(该列表具有类型和ip地址)。当我使用“w”时,文件内容仅在最后一行,但当我使用“a”时,它将保存列表中所有设备的所有内容,但当我再次运行脚本时,它将继续
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()
第二次运行脚本时…该文件包含重复项,如下所示
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()
剧本如下
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
我真的希望有人能帮助我。。请帮忙。谢谢。使用模式
'w'打开文件将截断文件(如果存在),而模式'a'
将附加到文件
您的问题是,您正在以模式'w'
为每行输出重新打开文件。每次重新打开文件时,都会删除以前的内容
此问题有两种解决方案:
首选的解决方案是在脚本中打开文件一次,然后使用现有的文件句柄向其写入多行,而不是为每行输出打开一个新的文件句柄
您也可以在脚本开始时使用mode'w'
打开文件一次,然后从那时起使用mode'a'
(但这是一种黑客行为)
实现第一个选项的一种方法是在主循环之前打开文件:
with open("outputFile.txt", "w") as f:
for line in deviceFile:
# ...
# Replace this line:
# writeOutFile(outFileName, sendCommand)
# With this one:
f.write(sendCommand)
在写入此字符串之前,您可能需要将换行符(“\n”
)追加到此字符串;我看不出dvs\u connect.send\u command()
如何格式化其输出。问题不在于以不同的模式打开文件,你可以做的是将文件信息保存在pickle文件中,当你运行它时,如果你想存储设备的ip,请检查它是否存在于pickle dump文件中,如果是这样的话,跳过else写入文本文件,并在最后用输出更新pickle文件Hanks先生…你能展示一下它是如何完成的吗?谢谢你的回答先生…选项1是我尝试做的。。。我已经尝试过在for循环中打开文件,但还没有在for循环之前打开文件…感谢您的回复和示例。我试试这个。