0
def Delete_con():
    contact_to_delete= input("choose name to delete from contact")
    to_Delete=list(contact_to_delete)
    with open("phonebook1.txt", "r+") as file:
        content = file.read()
        for line in content:
            if not any(line in line for line in to_Delete):
                content.write(line)

I get zero error. but the line is not deleted. This function ask the user what name he or she wants to delete from the text file.

Kevin
  • 959
  • 10
  • 14
  • Use proper indentation please – CIsForCookies Mar 20 '18 at 16:36
  • Your to_delete variable is a list of chars. I'm going to assume that you are not going to be deleting a line from a phonebook based on if a letter matches a line from the phonebook. – dfundako Mar 20 '18 at 16:43
  • Possible duplicate of [Deleting a specific line in a file (python)](https://stackoverflow.com/questions/4710067/deleting-a-specific-line-in-a-file-python) – nj2237 Mar 20 '18 at 16:47

2 Answers2

0

This should help.

def Delete_con():
    contact_to_delete= input("choose name to delete from contact")
    contact_to_delete = contact_to_delete.lower()   #Convert input to lower case
    with open("phonebook1.txt", "r") as file:
        content = file.readlines()              #Read lines from text
        content = [line for line in content if contact_to_delete not in line.lower()]   #Check if user input is in line 
    with open("phonebook1.txt", "w") as file:    #Write back content to text 
        file.writelines(content)
Rakesh
  • 81,458
  • 17
  • 76
  • 113
0

Assuming that:

  • you want the user to supply just the name, and not the full 'name:number' pair
  • your phonebook stores one name:number pair per line

I'd do something like this:

import os
from tempfile import NamedTemporaryFile


def delete_contact():
    contact_name = input('Choose name to delete: ')
    # You probably want to pass path in as an argument
    path = 'phonebook1.txt'
    base_dir = os.path.dirname(path)
    with open(path) as phonebook, \
            NamedTemporaryFile(mode='w+', dir=base_dir, delete=False) as tmp:
        for line in phonebook:
            # rsplit instead of split supports names containing ':'
            # if numbers can also contain ':' you need something smarter
            name, number = line.rsplit(':', 1)
            if name != contact_name:
                tmp.write(line)
        os.replace(tmp.name, path)

Using a tempfile like this means that if something goes wrong while processing the file you aren't left with a half-written phonebook, you'll still have the original file unchanged. You're also not reading the entire file into memory with this approach.

os.replace() is Python 3.3+ only, if you're using something older you can use os.rename() as long as you're not using Windows.

Here's the tempfile documentation. In this case, you can think of NamedTemporaryFile(mode='w+', dir=base_dir, delete=False) as something like open('tmpfile.txt', mode='w+'). NamedTemporaryFile saves you from having to find a unique name for your tempfile (so that you don't overwrite an existing file). The dir argument creates the tempfile in the same directory as phonebook1.txt which is a good idea because os.replace() can fail when operating across two different filesystems.

Kevin
  • 959
  • 10
  • 14
  • thanks.. i see unfamiliar code in your example. please explain this NamedTemporaryFile(mode='w+', dir=base_dir, delete=False) as tmp: – Franky Padilla Mar 22 '18 at 13:58
  • I've edited the answer to add a link to the tempfile docs and a brief explanation of NamedTemporaryFile's use in this example. – Kevin Mar 22 '18 at 17:55