【发布时间】:2021-01-19 22:34:18
【问题描述】:
我试图用 io 模块覆盖一行文本,问题是当我运行我的脚本并检查我的 .txt 时,我覆盖的行很好,除了文件末尾。
from io import open
text = open("tst.txt", "r+")
list_j = text.readlines()
list_j[0] = "Modified\n"
text.seek(0)
text.writelines(list_j)
text.close
txt 文件之前
first line of text
second line of text
third line of text
fourth line of text
txt 文件后
Modified
second line of text
third line of text
fourth line of text
of text
【问题讨论】:
标签: python list module io overwrite