📌  相关文章
📜  如何替换文本文件python代码示例的第一行

📅  最后修改于: 2022-03-11 14:46:11.730000             🧑  作者: Mango

代码示例1
with open("test.txt") as f:
    lines = f.readlines()

lines # ['This is the first line.\n', 'This is the second line.\n']

lines[0] = "This is the line that's replaced.\n"

lines # ["This is the line that's replaced.\n", 'This is the second line.\n']

with open("test.txt", "w") as f:
    f.writelines(lines)