#Book Management Mini program.
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:”)
Isbn= input(“Enter ISBN of the book:”)
Genre= input(“Enter genre of the book:”)
Pages= int(input(“Enter number of pages of the book:”))
Publisher= input(“Enter publisher of the book:”)
Year= int(input(“Enter publication year of the book:”))
S[‘Title’]= title
S[‘Author’]= author
S[‘ISBN’]= isbn
S[‘Genre’]= genre
S[‘Pages’]= pages
S[‘Publisher’]= publisher
S[‘Publication Year’]= year
Print(s)
Pickle.dump(s,f)
Ans= input(“Do you want to add more records (y/n):”)
f.close()
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’)
isbn=input(“Enter ISBN of the book to be searched:”)
print(‘\n’)
try:
while True:
s=pickle.load(f)
if s[‘ISBN’]==isbn:
found=’t’
print(s)
except EOFError:
if found==’t’:
print(“Book found.\n”)
else:
print(‘Book not found.\n’)
f.close()
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)
title= input(“Enter book’s title:”)
author= input(“Enter author’s name:”)
isbn= input(“Enter ISBN of the book:”)
genre= input(“Enter genre of the book:”)
pages= int(input(“Enter number of pages of the book:”))
publisher= input(“Enter publisher of the book:”)
year= int(input(“Enter publication year of the book:”))
s[‘Title’]= title
s[‘Author’]= author
s[‘ISBN’]= isbn
s[‘Genre’]= genre
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”)
print(“4. Update a book record”)
print(“5. Delete a book record”)
print(“6. Exit”)
ch= int(input(“Enter your choice (1-6):”))
if ch==1:
print(“Add new book record”)
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.”)