Search
 
SCRIPT & CODE EXAMPLE
 

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()
    f.seek(0)
    for line in new_f:
        if searched not in line:
            f.write(line)
    f.truncate() # close your file
Comment

PREVIOUS NEXT
Code Example
Python :: with urllib.request.urlopen("https:// 
Python :: django rest documentation 
Python :: pynput.keyboard.Key 
Python :: numpy remove element 
Python :: how to make html files open in chrome using python 
Python :: R write dataframe to file 
Python :: how to check if email exists in python 
Python :: Getting the Current Working Directory in Python 
Python :: how to print palindrome in 100 between 250 in python 
Python :: python dictionary get key by value 
Python :: multirow np.rand.randint 
Python :: datetime to unix timestamp milliseconds python 
Python :: python absolute value 
Python :: numpy standard deviation 
Python :: Read XML file to Pandas DataFrame 
Python :: swap list items in python 
Python :: find average of list python 
Python :: how to get decimal part of a double in python 
Python :: python remove nan rows 
Python :: pathlib path exists 
Python :: Installing packages from requirements.txt file 
Python :: UTC to ISO 8601: 
Python :: shutil copyfile python 
Python :: unique id python 
Python :: python os.name mac 
Python :: python space separated input 
Python :: python 2 decimal places format 
Python :: move one column value down by one column in pandas 
Python :: Renaming an index in pandas data frame 
Python :: python raise and exit 
ADD CONTENT
Topic
Content
Source link
Name
4+3 =