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.