📌  相关文章
📜  如何删除文件中的特定行 - Python 代码示例

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

代码示例1
searched = "snow" # every line containing the variable "searched" will be deleted

with open("file.txt","r+") as f: # open your file
    new_f = f.readlines()
    f.seek(0)
    for line in new_f:
        if searched not in line:
            f.write(line)
    f.truncate() # close your file