0

I'm creating a Contact list/book program which can create new contacts for you. Save them in a 'txt' file. List all contacts, and delete existing contacts. Well sort of. In my delete function there is an error which happens and I can't quite tell why?. There isn't a error prompted on the shell when running. It's meant to ask the user which contact they want to delete, find what the user said in the 'txt' file. Then delete it. It can find it easily, however it just deletes everything.

I've tried various methods of doing this but nothing works?

import os, time, random, sys, pyautogui


#function for creating a new contact.
def new_contact():
    os.system('cls')


    name = str(input("Clients name?\n:"))

    name = name + " -"

    info = str(input("Info about the client?\n:"))

    #starts formatting clients name and info for injection into file.

    total = "\n\n"
    total = total + name
    total = total + " "
    total = total + info
    total = total + "\n"
    #Injects info into file.


    with open("DATA.txt", "a") as file:

        file.write(str(total))
        file.close

    main()

#function for listing ALL contacts made.
def list():
    os.system('cls')



    file = open("DATA.txt", "r")
    read = file.read()
    file.close


    #detects whether there are any contacts at all. If there are none the only str in the file is "Clients:"
    if read == "Clients:":
        op = str(input("You havn't made any contacts yet..\nDo you wish to make one?\n:"))


        if op == "y":
            new_contact()


        else:
            main()

    else:
        print (read)
        os.system('pause')
        main()


#Function for deleting contact
def delete_contact():
    os.system('cls')


    file = open("DATA.txt", "r")
    read = file.read()
    file.close


    #detects whether there are any contacts at all. If there are none the only str in the file is "Clients:"
    if read == "Clients:":
        op = str(input("You havn't made any contacts yet..\nDo you wish to make one?\n:"))


        if op == "y":
            new_contact()


        else:
            main()


    else:
        #tries to delete whatever was inputted by the user.
        file = open("DATA.txt", "r")
        read = file.read()
        file.close


        print (read, "\n")


        op = input("copy the Clients name and information you wish to delete\n:")


        file = open("DATA.txt", "w")
        var = read.replace((op), "")
        file.write(var)
        file.close
        os.system('pause')
        main()

        #with open("DATA.txt") as f:
            #reptext=f.read().replace(op, '')


        #with open("DATA.txt", "w") as f:
            #f.write(reptext)
            #main()


main()

It is expected to delete whatever the user inputs as the variable op from the text file however it just deletes everything. It works by making everything inside the text file a str, replacing what the user inputted in the str with a . However, it just replaces everything in the file with (nothing).

Here is what is in the .txt file:

Clients:

Erich - The developer

Bob - the test dummy.
Arnav Poddar
  • 354
  • 2
  • 18
  • 1
    Nitpick: Should be `file.close()`. However, there are times where you use `with` to open up the file which will automatically close the file and other times where you don't do that. – rayryeng Jun 09 '19 at 22:42

1 Answers1

0

As @rayryeng said in his comment, the error is probably generating from the fact that you are not closing the file properly in the delete_contact() function of your code. The entire function should be like this: (BTW I deleted the main menu function of your code because it was not necessary to this post KEEP IT IN YOUR CODE)

def delete_contact():
    os.system('cls')


    file = open("DATA.txt", "r")
    read = file.read()
    file.close()


    #detects whether there are any contacts at all. If there are none the only str in the file is "Clients:"
    if read == "Clients:":
        op = str(input("You havn't made any contacts yet..\nDo you wish to make one?\n:"))


        if op == "y":
            new_contact()


        else:
            main()


    else:
        #tries to delete whatever was inputted by the user.
        file = open("DATA.txt", "r")
        read = file.read()
        file.close() #Edited this to make the file close properly


        print (read, "\n")


        op = input("copy the Clients name and information you wish to delete\n:")


        file = open("DATA.txt", "w")
        var = read.replace((op), "")
        file.write(var)
        file.close() #Edited this to make it close properly
        os.system('pause')
        main()

HOWEVER, THIS IS NOT THE END

If you use the code I posted above, your code will not run as you want it to. Instead, it will have the previous contacts (before the deletion) and then the list of contacts/clients minus the contact that the user deleted. To fix this, add this segment of code right before you open the file for the last time:

file = open("DATA.txt", "w")
file.replace(read, "")
file.close()

What this code does is it opens the doc, replaces the previous contents with (nothing), and then closes it (properly).

(Nice program by the way)

Arnav Poddar
  • 354
  • 2
  • 18