0% found this document useful (0 votes)
40 views

Binary File Program

Uploaded by

kalraprahas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Binary File Program

Uploaded by

kalraprahas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

import pickle

# Define the structure of a Book


class Book:
def __init__(self, book_no, book_name, author, price):
self.book_no = book_no
self.book_name = book_name
self.author = author
self.price = price

# Function to create and write records to the binary file


def create_file():
with open('Book.dat', 'ab') as file:
for _ in range(10): # Input 10 records
book_no = int(input("Enter Book Number: "))
book_name = input("Enter Book Name: ")
author = input("Enter Author Name: ")
price = float(input("Enter Price: "))
book = Book(book_no, book_name, author, price)
pickle.dump(book, file)

# Function to display all book details


def display():
try:
with open('Book.dat', 'rb') as file:
print("\nBook Details:")
while True:
try:
book = pickle.load(file)
print(f"Book No: {book.book_no}, Name:
{book.book_name}, Author: {book.author}, Price: {book.price}")
except EOFError:
break
except FileNotFoundError:
print("Book.dat file not found.")

# Function to count and display records by a specific author


def count_rec(author):
count = 0
try:
with open('Book.dat', 'rb') as file:
print(f"\nBooks by Author '{author}':")
while True:
try:
book = pickle.load(file)
if book.author.lower() == author.lower():
print(f"Book No: {book.book_no}, Name:
{book.book_name}, Price: {book.price}")
count += 1
except EOFError:
break
print(f"\nTotal books by '{author}': {count}")
return count
except FileNotFoundError:
print("Book.dat file not found.")
return 0

# Function to update the price of a book by book number


def update(book_no):
books = []
try:
with open('Book.dat', 'rb') as file:
while True:
try:
book = pickle.load(file)
books.append(book)
except EOFError:
break

found = False
for book in books:
if book.book_no == book_no:
book.price *= 1.1 # Increase price by 10%
found = True

if found:
with open('Book.dat', 'wb') as file:
for book in books:
pickle.dump(book, file)
print(f"Updated price of Book No {book_no} to
{book.price:.2f}")
else:
print(f"Book No {book_no} not found.")
except FileNotFoundError:
print("Book.dat file not found.")

# Main menu function


def main_menu():
while True:
print("\nMenu:")
print("1. Create File")
print("2. Display Books")
print("3. Count Books by Author")
print("4. Update Book Price")
print("5. Exit")
choice = input("Enter your choice: ")

if choice == '1':
create_file()
elif choice == '2':
display()
elif choice == '3':
author = input("Enter Author Name to count: ")
count_rec(author)
elif choice == '4':
book_no = int(input("Enter Book No to update price: "))
update(book_no)
elif choice == '5':
print("Exiting the program.")
break
else:
print("Invalid choice, please try again.")

if __name__ == "__main__":
main_menu()

'''
Menu:
1. Create File
2. Display Books
3. Count Books by Author
4. Update Book Price
5. Exit
Enter your choice: 2

Book Details:
Book No: 1001, Name: Midnight's Children, Author: Salman Rushdie, Price:
29.99
Book No: 1002, Name: The Satanic Verses, Author: Salman Rushdie, Price:
39.23
Book No: 1001, Name: Midnight's Children, Author: Salman Rushdie, Price:
29.99

Menu:
1. Create File
2. Display Books
3. Count Books by Author
4. Update Book Price
5. Exit
Enter your choice: 3
Enter Author Name to count: Salman Rushdie

Books by Author 'Salman Rushdie':


Book No: 1001, Name: Midnight's Children, Price: 29.99
Book No: 1002, Name: The Satanic Verses, Price: 39.23
Book No: 1001, Name: Midnight's Children, Price: 29.99

Total books by 'Salman Rushdie': 3

Menu:
1. Create File
2. Display Books
3. Count Books by Author
4. Update Book Price
5. Exit
Enter your choice: 4
Enter Book No to update price: 1001
Updated price of Book No 1001 to 32.99

Menu:
1. Create File
2. Display Books
3. Count Books by Author
4. Update Book Price
5. Exit
Enter your choice: 5
Exiting the program.

'''

You might also like