Class 12 Programs
- 1.Read text file line by line with # separator
- 2.Count vowels, consonants, uppercase, lowercase in file
- 3.Remove lines containing character 'a'
- 4.Binary file with name and roll number search
- 5.Binary file update marks by roll number
- 6.Random number generator (Dice Simulator)
- 7.Stack implementation using list
- 8.CSV file with user-id and password
- 9.User Defined Functions to manipulate List Store Indices of Non Zero elements &Double the Odd values
- 10.WORKING WITH BINARY FILE IN PYTHON Create a binary file, Search and display the records from the binary file
- 11.TEXT FILES IN PYTHON Remove duplicate lines from the file & Display unique words present in the file
- 12.TEXT FILES IN PYTHON Copying lines to a new file & Replacing the '-' sign with a blankspace
- 13.TEXT FILES IN PYTHON Count the lines starts with I/T & Display the lines with exactly 6 words
- 14.TEXT FILES IN PYTHON Count the occurrences of word & Count number of vowels and consonants
- 15.User Defined Functions to Manipulate List Find the Longest Word and Shifting the elements to left
- 16.IMPLEMENTATION OF STACK USING LIST IN PYTHON
- 17.ALTER table to add new attributes / modify data type / drop attribute
- 18.UPDATE table to modify data
- 19.ORDER By to display data in ascending / descending order
- 20.DELETE to remove tuple(s)
- 21.GROUP BY and find the min, max, sum, count and average
- 22.INTERFACING PYTHON WITH MYSQL DATABASE CONNECTIVITY APPLICATION PROGRAM - DELETE RECORDS
- 23.INTERFACING PYTHON WITH MYSQL DATABASE CONNECTIVITY APPLICATION PROGRAM - UPDATE RECORDS
- 24.INTERFACING PYTHON WITH MYSQL DATABASE CONNECTIVITY APPLICATION PROGRAM - INSERT RECORDS
Quick Tips
- • Import random module for number generation
- • Use randint(1, 6) for dice simulation
- • Handle user input validation properly
Binary File Search
AIM
To write a Python program to search for a specific record in a binary file and display the matching records.
ALGORITHM
- Start
- Import pickle module for binary file operations
- Open the binary file in read mode
- Load records from the file using pickle.load()
- Search for the specific record based on criteria
- Display matching records
- Close the file
- Stop
PROGRAM
import pickle
def search_record():
try:
# Open binary file for reading
with open('student.dat', 'rb') as file:
print("Searching for records...")
search_name = input("Enter name to search: ")
found = False
try:
while True:
record = pickle.load(file)
if record['name'].lower() == search_name.lower():
print(f"Record Found:")
print(f"Name: {record['name']}")
print(f"Roll No: {record['roll']}")
print(f"Marks: {record['marks']}")
print("-" * 30)
found = True
except EOFError:
pass
if not found:
print("Record not found!")
except FileNotFoundError:
print("File not found!")
except Exception as e:
print(f"Error: {e}")
# Main program
if __name__ == "__main__":
search_record()OUTPUT
Searching for records...
Enter name to search: John
Record Found:
Name: John
Roll No: 101
Marks: 85
------------------------------
CONCLUSION
Thus, the program to search records in a binary file was successfully executed and the output was verified.
VIVA QUESTIONS
What is a binary file?
A binary file stores data in binary format and is not human-readable. It's more efficient for storing structured data.
What is the pickle module used for?
The pickle module is used for serializing and deserializing Python objects to and from binary format.