python-俩个文件的合并
这个脚本只支持俩个文件行数相同。逻辑是将俩文件保存到俩列表,通过行数循环,分别取出俩列表的每一行,相加,重新输出文件。
file1path = '1createip.txt'
file2path = '主机名list.txt'
file_1 = open(file1path,'r')
file_2 = open(file2path,'r')
list1 = []
for line in file_1.readlines():
ss = line.strip()
list1.append(ss)
file_1.close()
list2 = []
for line in file_2.readlines():
ss = line.strip()
list2.append(ss)
file_2.close()
file_new = open('2ip加主机名.txt','w')
for i in range(len(list1)):
sline = list1[i] + ' ' + list2[i]
file_new.write(sline+'\n')
file_new.close()