Answers for "delete specific line in file python"


how to delete a specific line in a file

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()
    for line in new_f:
        if searched not in line:
    f.truncate() # close your file
Posted by: Guest on February-08-2021

Code answers related to "delete specific line in file python"

Python Answers by Framework

Browse Popular Code Answers by Language