-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMODIFY RECORDS IN BINARY FILE.py
More file actions
71 lines (66 loc) · 2.18 KB
/
Copy pathMODIFY RECORDS IN BINARY FILE.py
File metadata and controls
71 lines (66 loc) · 2.18 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import pickle
def Create_Records():
with open("binaryfile.dat", "wb") as file:
while True:
name = input("Enter name: ")
age = int(input("Enter age: "))
city = input("Enter city: ")
record = [name, age, city]
pickle.dump(record, file)
choice = input("Do you want to add more records? (y/n): ")
if choice.lower() != "y":
break
def Search_Records():
with open("binaryfile.dat", "rb") as file:
name = input("Enter name to search: ")
while True:
try:
record = pickle.load(file)
if record[0] == name:
print("Name: ", record[0])
print("Age: ", record[1])
print("City: ", record[2])
break
except EOFError:
break
def Modify_Records():
with open("binaryfile.dat", "rb+") as file:
name = input("Enter name to modify: ")
while True:
try:
record = pickle.load(file)
if record[0] == name:
record[1] = int(input("Enter new age: "))
record[2] = input("Enter new city: ")
file.seek(-len(record), 1)
pickle.dump(record, file)
break
except EOFError:
break
def Delete_Records():
with open("binaryfile.dat", "rb+") as file:
name = input("Enter name to delete: ")
while True:
try:
record = pickle.load(file)
if record[0] == name:
file.seek(-len(record), 1)
file.write(b"\0")
break
except EOFError:
break
def Display_Records():
with open("binaryfile.dat", "rb") as file:
while True:
try:
record = pickle.load(file)
print("Name: ", record[0])
print("Age: ", record[1])
print("City: ", record[2])
except EOFError:
break
Create_Records()
Search_Records()
Modify_Records()
Delete_Records()
Display_Records()