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

4.33
6

                                    # Basic syntax:
sed 'line#d' input_file
# Where line# is the line you want to delete from the input_file

# Example usage:
input_file contains:
this is
my favorite
line

sed '2d' input_file
--> this is
--> line

4.33 (6 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
delete specific line in text file python linux delete specific line from file delete specific line in bash how to remove specific line from file python how to delete specific line in file python python delete certain line in file how to delete specific line in text file python delete specific line from a file python how to remove specific lines from a file in linux how to delete specific line in file how to delete a particular line in a file using python how to delete certain lines from a file in python how to delete a specific line in file python bash delete line in file how to remove particular line from file in python delete a line from specific file bash delete line from a file unix delete specific lines from file remove specific line from file linux delete lines in file shell script delete in file specific line shell shell delete line from file how to delete lines from a file in bash delete specific lines in file python delete certain lines from a file in unix Removing a line from a file which has specific character in shell Removing a line from a file which has specific character linux remove specific line from file bash delete specific line pattern from file bash delete specific line from file how to delete a specific line in a file in python how to delete specific file lines python remove certain line from a file delete specific line from file unix hwo to delete certain lines ina file linux command how to remove a specific line from a file python delete line from file in bash remove the line containing some specific string from a file in bash bash remove lasst tewo lines of file remove particular line from files sed delete lines by line number how to delete a specific line in a file delete specific line from file linux remove line from file bash shell delete some line how to delete a particular line in shell script bash remove current file linux delete line from file by number how to remove lines from a file how to remove a line from a file shell bash remove line from all files how to delte a line from a text file using linux sed remove lines containing string if file contains specific word, delete file bash deleting the contents of a line and enetering new text linux delete line of file linux how to delete line of text in linux remove a specific line from a file linux how to remove a line number in a file linux using set how to remove a line in a file linux using set how to remove a line in a file linux how to delete particular line and range of line in sed delete line from file unix sed remove line delete second line of file unix without using sed delete second line of file unix remove a line from a file bash how to remove a line from a file in bash remove row unix command bash remoce line from file remove current linein file bash sh delete line from file how to remove specefic line email using kali how to remove certain line thath have word in kali terminal command line delete line from file how to remove a line form file using terminal how to remove lines from teh file int he terminal delete one line in a file using bash remove line number with sed delete a specific line sed delete specific line in file bash command line delete text unix command delete a line in a file delete a line from file in shell script bash how to delete a specific line from a file
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source