我可以打开文件“安装程序.conf,将文本从“Hostname=server”替换为“Hostname=server2”,并将其另存为“setup2.conf”。你知道吗
但是,我希望每一行列表.txt成为新文件的名称,而不是“setup2.conf”。
“的内容”列表.txt“:”
server1
server2
server3
例如,我读了每一行:
fh = open('list.txt')
while True:
line = fh.readline()
print(line)
if not line:
break
fh.close()
替换文本并保存文件的示例:
fin = open("setup.conf", "rt")
fout = open("setup2.conf", "wt")
for line in fin:
fout.write(line.replace('Hostname=server1', 'Hostname=server2'))
fin.close()
fout.close()
为每个读取行打开一个新文件,然后写入。例如
相关问题 更多 >
编程相关推荐