Monday 18 September 2017

How to remove rows with specific text from the notepad using python 2.7

Below script is used to remove specific text from notepad.

Below script is used to remove specific text from the notepad using python. To demonstrate the same we have used a file which has “networking” word in the file.

#open the file in read mode which you need to modify, we have used ip.txt file.
file= open("ip.txt","r")
# Read the file
lines = file.readlines()
# close the file
file.close()

# Read the file in write mode
file = open("ip.txt","w")
# Reading each line and writing text, which is not a "networking" word, back to the file.
for line in lines:
    if line!="networking"+"\n":
        file.write(line)
file.close()

Before the script execution, ip.txt contains below content. You can see that below file contains word "networking" in many rows and our onjective is to remove all the rows which contains word "networking".






















After script execution, all rows containing  word "networking" has been deleted.

  

No comments:

Post a Comment