fo_r = open("xxx.log", "r")
content = fo_r.read()
fo_r.close()
pos = content.find("name:")
data = lilei
if (pos != -1):
pos = pos + 5
content = content[:pos] + data + "\n" + content[pos:]
fo_w = open("xxx.log", "w")
fo_w.write(content)
fo_w.close()
这个代码的作用是打开xxx.log找到 name:这个位置,(使用content.find())这个找的的位置是在name:前面。我们想在冒号后面加入一个名字lilei,所以要在这个位置后面+5然后重新写入xxx.log