0% found this document useful (0 votes)
58 views4 pages

Python Binary File Student Records

The document describes two Python programs that manage student records using binary files. The first program allows users to create a binary file with student names and roll numbers, search for a roll number, and display the corresponding name. The second program extends this functionality by including marks, allowing users to update marks for a specific roll number and displaying all student records.

Uploaded by

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

Python Binary File Student Records

The document describes two Python programs that manage student records using binary files. The first program allows users to create a binary file with student names and roll numbers, search for a roll number, and display the corresponding name. The second program extends this functionality by including marks, allowing users to update marks for a specific roll number and displaying all student records.

Uploaded by

dvishva18
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PYTHON PROGRAM IN BINARY FILE – I

Aim:-
To create a binary file with name and roll number. Search for a given
roll number and display the name, if not found display appropriate
message.

Program:-

import pickle
def write():
students = []
while True:
r = int(input("Enter Roll No: "))
n = input("Enter Name: ")
record = [r, n]
[Link](record)

ch = input("More ? (Y/N): ")


if ch in 'Nn':
break
with open("[Link]", "wb") as f:
[Link](students, f)
print("Records saved successfully!\n")

def search():
rollno = int(input("Enter Roll No whose name you want to display: "))
found = 0
with open("[Link]", "rb") as f:
students = [Link](f)
for rec in students:
if rec[0] == rollno:
print("Name:", rec[1])
found = 1
break
if found == 0:
print("Sorry, not Found...")

write()
search()
Shell Output:
Enter Roll No: 01
Enter Name: Melki
More ? (Y/N): y
Enter Roll No: 02
Enter Name: Mite
More ? (Y/N): y
Enter Roll No: 03
Enter Name: V C
More ? (Y/N): n
Records saved successfully!

Enter Roll No whose name you want to display: 03


Name: V C

Enter Roll No whose name you want to display: 04


Sorry, not Found…

Result:
Thus the program was coded and executed successfully.
PYTHON PROGRAM IN BINARY FILE – II
Aim:-
To create a binary file with roll number, name and marks. Input a roll
number and update the marks.

Program:-

import pickle
def Write():
students = []
while True:
r = int(input("Enter Roll No: "))
n = input("Enter Name: ")
m = float(input("Enter Marks: "))
record = [r, n, m]
[Link](record)
ch = input("Do you want to enter more? (Y/N): ")
if ch in 'Nn':
break
with open("[Link]", "wb") as f:
[Link](students, f)
print("\nRecords saved successfully!\n")

def Read():
print("\n--- Student Records ---")
with open("[Link]", "rb") as f:
students = [Link](f)
for rec in students:
print("Roll No:", rec[0], "| Name:", rec[1], "| Marks:", rec[2])

def Update():
rollno = int(input("\nEnter Roll No whose marks you want to update: "))
with open("[Link]", "rb") as f:
students = [Link](f)
found = False
for rec in students:
if rec[0] == rollno:
print("Old Marks:", rec[2])
new_marks = float(input("Enter Updated Marks: "))
rec[2] = new_marks
found = True
break
if found:
with open("[Link]", "wb") as f:
[Link](students, f)
print("Marks updated successfully!")
else:
print("No record found with Roll No:", rollno)

Write()
Read()
Update()
Read()

Shell Output:-

Enter Roll No: 01


Enter Name: Melki
Enter Marks: 95
Do you want to enter more? (Y/N): y
Enter Roll No: 02
Enter Name: Mite
Enter Marks: 92
Do you want to enter more? (Y/N): y
Enter Roll No: 03
Enter Name: V C
Enter Marks: 100
Do you want to enter more? (Y/N): n
Records saved successfully!
--- Student Records ---
Roll No: 1 | Name: Melki | Marks: 95.0
Roll No: 2 | Name: Mite | Marks: 92.0
Roll No: 3 | Name: V C | Marks: 100.0
Enter Roll No whose marks you want to update: 2
Old Marks: 92.0
Enter Updated Marks: 98
Marks updated successfully!
--- Student Records ---
Roll No: 1 | Name: Melki | Marks: 95.0
Roll No: 2 | Name: Mite | Marks: 98.0
Roll No: 3 | Name: V C | Marks: 100.0

Result:-
Thus the program was coded and executed successfully.

Common questions

Powered by AI

In the first Python program, the search operation specifically targets the student's name by roll number. It reads all records from the binary file at once, then iterates to find the matching roll number . The second program lacks an equivalent standalone search operation for just name retrieval, focusing instead on updating marks, which inherently verifies record existence during the update process .

The use of binary files in the Python programs offers efficient storage, as binary files usually consume less space and can be processed quickly, essential for large datasets. However, a limitation is the lack of human readability, which makes direct modifications challenging. Any updates or searches require specific code to deserialize and manage the data, adding complexity to operations that might be simpler with plain text files .

Overall efficiency and functionality could be optimized by implementing features such as batch processing of records, using in-memory data structures like dictionaries for faster searches and updates by roll number, or leveraging databases for even more efficient data management. Additionally, incorporating more advanced file handling techniques and parallel processing could significantly reduce file access times and increase the program's responsiveness, especially with larger datasets .

The first Python program searches for a student's name by roll number within a binary file. If the entered roll number is not found in the records, the program sets a flag 'found' to 0 and displays the message 'Sorry, not Found...' indicating that no matching record was found .

Both programs ensure data consistency by overwriting the data file with the entire updated dataset each time a change is made, ensuring that the file contents always accurately reflect the current state of the data list in memory. To enhance consistency, features such as transaction logs before updates, or implementing backup copies before overwrites, could help prevent data loss or corruption .

The 'Write' function in the first program allows users to input only roll numbers and names for students, storing this information in a binary file, with the process continuing until the user indicates they do not wish to add more entries. In contrast, the second program's 'Write' function also accepts marks alongside roll numbers and names, providing a more comprehensive data entry mechanism. Both programs use a similar iterative loop structure that is controlled based on user input .

Input validation and user prompts guide the user through the data entry and manipulation processes, ensuring that the correct types and formats of data are provided, such as asking for integers for roll numbers and floats for marks. They help detect and prevent common input errors and give feedback on process outcomes, such as whether a data entry was successful or a specific record was found, improving the user experience significantly .

The 'pickle' module is used for serializing and deserializing Python object structures, allowing the storage of the list of student records in a binary file format. It simplifies data handling between a running application and storage, auto-converting complex data types to a storable format. A significant security consideration is that 'pickle' is not secure against code execution risks if the file source is untrusted, as it can execute arbitrary code during unpickling. Therefore, it's important to only load data from trusted sources .

While the programs currently do not employ extensive error handling mechanisms, improvements could include using try-except blocks to manage file access or data conversion issues, such as handling cases where the 'pickle.load()' operation fails due to file corruption or when user inputs are invalid (e.g., non-integer values for roll numbers). Employing these techniques would make the programs more robust and user-friendly .

The second Python program updates student marks by first reading the records from a binary file into a list. It iterates through the list to find the student with the specified roll number and updates their marks when the roll number matches. The updated records are written back to the file, ensuring the file reflects the changes. File integrity is maintained by loading all records, modifying the necessary entry, and saving the complete dataset back to the file .

You might also like