-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCR_D.py
37 lines (30 loc) · 1.12 KB
/
CR_D.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#inputing contacts
filename ="exam.txt"
n = int(input("enter the number of contacts you would like to save\n"))
file = open(filename, "a")
for i in range(n):
cont = (input("enter name and phone number respectively:\n"))
file.write(cont + "\n")
file.close
#searching for contacts
word = input("insert the name you would like to search for\n")
with open("exam.txt", "r") as file:
for line_number, line in enumerate(file, start=1):
if word in line:
print(f"Word '{word}' found on line {line_number}")
break
print("Search completed.")
#deleting contacts
# deleting a string/contact
with open('exam.txt', 'r') as fr:
# Get all the line/contacts from the file
lines = fr.readlines()
# Get the string to be deleted.
delete = input("insert the name you would like to delete: \n")
if not (delete == "" ):
with open('exam.txt', 'w') as fw:
for line in lines:
# This will put back all lines that don't start with the provided string.
if not (line.startswith(delete)):
fw.write(line)
print("Deleted")