BINARY project
import pickle
#writing data into new binary file
def write_file():
with open('parth_books.dat','wb') as write_data:
List=[]
while True:
book_name=input('Enter Book Title: ').title()
book_author=input('Enter Book Author: ').title()
book_id=int(input('Enter Book ID: '))
book_cost=float(input('Enter Book Cost: '))
[Link]([book_name,book_author,book_id,book_cost])
ask_choice=input('Do you want to write more ? (Y/N) ')
if ask_choice.lower() == 'n':
break
[Link](List,write_data)
#displaying the data written in the file
def read_file():
try:
with open('parth_books.dat','rb') as read_data:
data_list=[Link](read_data)
for i in data_list:
print('Book Name :',i[0],'\nAuthor Name :',i[1],'\nBook ID :',i[2],'\nBook Price :',i[3])
print('\n')
except FileNotFoundError:
write()
#adding new records in the existing file
def append_file():
try:
with open('parth_books.dat','rb+') as add_data:
existing_records=[Link](dg)
new_records=[]
while True:
book_name=input('Enter Book Name: ')title()
author_name=input('Enter Book Author: ')title()
book_id=int(input('Enter Book ID.: '))
book_cost=float(input('Enter Book Cost: '))
new_records.append([book_name,author_name,book_id,book_cost])
add_choice=input('Do you want to add more records ? (Y/N) ')
if add_choice.lower()=='n':
break
existing_records.extend(new_records)
add_data.seek(0)
[Link](existing_records,add_data)
except FileNotFoundError:
write()
#searching for records stored in the file
def search_file():
try:
with open('parth_books.dat','rb') as search_data:
data_stored=[Link](search_data)
while True:
field_check=input('Enter Field to be checked (I=ID, N=name, A=author, P=price
. range) : ')
if field_check.lower()=='n':
BookName_search=input('Enter Book Name to be Searched: ')
for i in data_stored:
if i[0]==BookName_search:
print(i)
elif field_check.lower()=='a':
AuthorName_search=input('Enter Author Name to be Searched: ')
for i in data_stored:
if i[1]==AuthorName_search:
print(i)
elif field_check.lower()=='i':
BookID_search=int(input('Enter ID to be Searched: '))
for i in y:
if i[2]==BookID_search:
print(i)
elif field_check.lower()=='p':
start_price=float(input('Price Range Starting From: '))
end_price=float(input('Price Range Ending At: '))
if start_price<end_price:
for i in data_stored:
if i[3]>=start_price and i[3]<=end_price:
print(i)
choice_search=input('Do you want to search more ? (Y/N) ')
if choice_search.lower()=='n':
break
except FileNotFoundError:
write()
#updating data stored in the file
def update_file():
try:
with open('parth_books.dat','rb+') as update_data:
existing_data=[Link](update_data)
for i in existing_data:
print(i)
ask_modiffy=input('Do you want to modify the given record (Y/N) ? ')
if ask_modify.upper()=='Y':
field_modify=input(‘Enter Field to be modified (N=book name, A=author
. name, I=ID, P=price)?: ‘)
if field_modify.lower()=='n':
i[0]=input('Enter New Book Name: ')
elif field_modify.lower()=='a':
i[1]=input('Enter New Author Name: ')
elif field_modify.lower()=='i':
i[2]=int(input('Enter New Book ID: '))
elif field_modify.lower()=='p':
i[3]=float(input('Enter New Cost of Book: '))
update_data.seek(0)
[Link](existing_data,update_data)
except FileNotFoundError:
write()
#deleting a record from the binary file
def delete_file():
try:
with open('parth_books.dat','rb+') as del_data:
existing_data=[Link](del_data)
BookId_delete=int(input('Enter Book ID to be deleted: '))
for i in existing_data:
if i[2]==BookId_delete:
existing_data.remove(i)
del_data.seek(0)
[Link](existing_data,del_data)
except FileNotFoundError:
write()
#Menu driven program for navigating through all the functions
while True:
role=int(input('Are you a owner (1) or customer (2) ?: '))
if role==1:
print('Owner Menu')
print('[Link] data into new binary file')
print('[Link] the data written in the file')
print('[Link] new records in the existing file')
print('[Link] for records stored in the file')
print('[Link] data stored in the file')
print('[Link] a record from the binary file')
print('7. Exit Menu')
while True:
owner_choice=input('Enter What You Want To Do: ')
if owner_choice.lower()==1:
write_file()
elif owner_choice.lower()==2:
read_file()
elif owner_choice.lower()==3:
append_file()
elif owner_choice.lower()==4:
search_file()
elif owner_choice.lower()==5:
update_file()
elif owner_choice.lower()==6:
delete_file()
elif owner_choice.lower()==7:
break
else:
print('Invalid Choice')
elif role==2:
print('Customer Menu')
print('[Link] the data written in the file')
print('[Link] for records stored in the file')
print('[Link] Menu')
while True:
customer_choice=input('Enter what do you want to do: ')
if customer_choice.lower()==1:
read_file()
elif customer_choice.lower()==2:
search_file()
elif customer_choice.lower()==3:
break
else:
print('Invalid Choice’)
choice=input('Do You Want to Continue with more functions ? (Y/N) ‘)
if [Link]()=='n':
break
OUTPUT
Are you a owner (1) or customer (2) ?: 1
Owner Menu
[Link] data into new binary file
[Link] the data written in the file
[Link] new records in the existing file
[Link] for records stored in the file
[Link] data stored in the file
[Link] a record from the binary file
7. Exit Menu
Enter What You Want To Do: 1
Enter Book Title: cs python
Enter Book Author: sumita arora
Enter Book ID: 123
Enter Book Cost: 340
Do you want to write more ? (Y/N) y
Enter Book Title: simplified physics
Enter Book Author: sl arora
Enter Book ID: 230
Enter Book Cost: 500
Do you want to write more ? (Y/N) n
Enter What You Want To Do: 2
Book Name : Cs Python
Author Name : Sumita Arora
Book ID : 123
Book Price : 340.0
Book Name : Simplified Physics
Author Name : Sl Arora
Book ID : 230
Book Price : 500.0
Enter What You Want To Do: 3
Enter Book Name: concept of physics
Enter Book Author: hc verma
Enter Book ID.: 110
Enter Book Cost: 600
Do you want to add more records ? (Y/N) n
Enter What You Want To Do: 4
Enter Field to be checked (I=ID, N=name, A=author, P=price range) : i
Enter ID to be Searched: 123
['Cs Python ', 'Sumita Arora', 123, 340.0]
Do you want to search more ? (Y/N) n
Enter What You Want To Do: 5
['Cs Python ', 'Sumita Arora', 123, 340.0]
Do you want to modify the given record (Y/N) ? y
what you want to modify (N=book name, A=author name, I=ID,
P=price)?: p
Enter New Cost of Book: 350
['Simplified Physics ', 'Sl Arora', 230, 500.0]
Do you want to modify the given record (Y/N) ? n
['Concept Of Physics ', 'Hc Verma', 110, 600.0]
Do you want to modify the given record (Y/N) ? n
Enter What You Want To Do: 6
Enter Book ID to be deleted: 110
Enter What You Want To Do: 2
Book Name : Cs Python
Author Name : Sumita Arora
Book ID : 123
Book Price : 350.0
Book Name : Simplified Physics
Author Name : Sl Arora
Book ID : 230
Book Price : 500.0
Enter What You Want To Do: 7
Do You Want to Continue with more functions ? (Y/N) n