0

I want to delete a row of text from a text file. This is my code:

with open("contactbook.txt","r") as file:
     for num, line in enumerate(file, 1):
          if args.name in line:
               filedata = file.read()
               filedata = filedata.replace(line, " ") 
with open('contactbook.txt', 'w') as file:
     file.write(filedata)

The texts are arranged like this:

Name:Hello  Phone number: 9  Address: 9  E-mail: 9
Name:Hi  Phone number: 8  Address: 8  E-mail: 8

NOTE: The actual file contains a lot of rows, the one above just shows how it looks like.

So for example, if I want to delete the row containing "Hi", I only want the following to remain.

Name:Hello  Phone number: 9  Address: 9  E-mail: 9

NOTE: The user input is only "Hi" but it will delete the entire row where it is found.

My code works sometimes but there are times it ends up deleting everything (the entire content of the text file), what am I doing wrong and what's a better way to do this?

Rose
  • 127
  • 7
  • Does this answer your question? [Search and replace a line in a file in Python](https://stackoverflow.com/questions/39086/search-and-replace-a-line-in-a-file-in-python) – Pynchia Jul 27 '20 at 02:54
  • if you can use `sed/perl` it will be great – Onyambu Jul 27 '20 at 03:00
  • @Pynchia Sadly no :( That one only replaces one word, in my example it will only remove "Hi", but the the phone number and etc will still remain – Rose Jul 27 '20 at 08:23
  • @Onyambu oooh but I'm not really familiar with that one hahah sorry! I'm a beginner in computer programming, this is the first language that I'm really learning – Rose Jul 27 '20 at 08:26

2 Answers2

0

Try this it simple and easy

a_file = open("sample.txt", "r")
lines = a_file.readlines()
a_file.close()
a= int( input("enter the line which you want to delete:"))
del lines[a]
new_file = open("sample.txt", "w+")
for line in lines:
    new_file.write(line)
new_file.close()
Ujjwal Dash
  • 767
  • 1
  • 4
  • 8
  • This one will delete the lines in index 1 right? But I want it to be able to delete other lines too depending on what the user inputs. How can I do that? – Rose Jul 27 '20 at 08:35
  • Ohhh yea I guess I can do it that way too. But what I had in mind previously was that the user input will be a keyword like "Hi". Not the line number. – Rose Jul 27 '20 at 08:59
-1

You can use readlines() which will read the lines into a list.
Once the list is created you can apply list comprehension.

See below:

my_file = open("contactbook.txt", "r")
content_list = my_file.readlines()
print(content_list)
simple_new = [i for i in content_list if 'Hi' not in i]
print(simple_new)
with open("outfile.txt", "w") as outfile:
    outfile.write("".join(simple_new))

You can use regex if you want to have more options like Ignore Case, etc.

regex_pattern = '^((?!Hi).)*$'
moreoptions_new = [i for i in content_list if re.search(regex_pattern, i, re.IGNORECASE)]
print(moreoptions_new)
with open("outfile2.txt", "w") as outfile:
    outfile.write("".join(moreoptions_new))

Jinto Lonappan
  • 312
  • 2
  • 8
  • Reading the whole file in memory isn't recommended. You should read each line and write it to the output file according to the condition – Pynchia Jul 27 '20 at 02:50