Source Code
Source Code
Def add():
Import pickle
S={}
F=open(‘book.dat’,’wb’)
Ans=’y’
While ans== ‘y’:
Title= input(“Enter book’s title:”)
Author= input(“Enter author’s name:”)
S[‘Title’]= title
S[‘Author’]= author
S[‘ISBN’]= isbn
S[‘Genre’]= genre
S[‘Pages’]= pages
S[‘Publisher’]= publisher
def display():
import pickle
s={}
f=open(‘book.dat’,’rb’)
print(‘\n’)
try:
while True:
s=pickle.load(f)
print(s)
except EOFError:
f.close()
def search():
import pickle
s={}
found=’f’
f=open(‘book.dat’,’rb’)
found=’t’
print(s)
except EOFError:
if found==’t’:
print(“Book found.\n”)
else:
def update():
import pickle
s={}
found=”f”
f=open(‘book.dat’,’rb+’)
print(‘\n’)
isbn= input(“Enter ISBN of the book to be updated:”)
try:
while True:
pos=f.tell()
s=pickle.load(f)
if s[‘ISBN’]==isbn:
found=’t’
print(s)
s[‘Pages’]= pages
s[‘Publisher’]= publisher
s[‘Publication Year’]= year
print(s)
f.seek(pos)
pickle.dump(s,f)
break
except EOFError:
if found==’t’:
print(“Book updated.\n”)
else:
print(“Book not found.\n”)
f.close()
def delete():
import pickle
import os
s={}
found=’f’
f=open(‘book.dat’,’rb+’)
f1=open(‘book1.dat’,’wb+’)
print(‘\n’)
isbn=input(“Enter ISBN to delete the book:”)
try:
while True:
s=pickle.load(f)
if s[‘ISBN’]!=isbn:
pickle.dump(s,f1)
else:
found=’t’
except EOFError:
if found==’t’:
print(“Book found and deleted.\n”)
else:
print(“Book not found.\n”)
f.close()
f1.close()
os.remove(‘book.dat’)
os.rename(‘book1.dat’,’book.dat’)
ch=1
while ch:
print(“Book Management System\n”)
print(“1. Add a new book record”)
print(“2. Display all book records”)
print(“3. Search for a book”)
add()
elif ch==2:
print(“Display all book records”)
display()
elif ch==3:
search()
elif ch==4:
update()
elif ch==5:
delete()
elif ch==6:
print(“Exiting the program.”)
break
else:
print(“Invalid choice. Please enter a number between 1 and 6.”)