-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Expand file tree
/
Copy pathDeleting record in a binary file.py
More file actions
51 lines (41 loc) · 2.03 KB
/
Deleting record in a binary file.py
File metadata and controls
51 lines (41 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import pickle
from typing import List, Tuple
def delete_student_record() -> None:
"""
Delete a student record from the binary data file 'studrec.dat' based on the provided roll number.
This function performs the following operations:
1. Reads the current student records from 'studrec.dat'
2. Prompts the user to enter a roll number to delete
3. Removes the record with the specified roll number
4. Writes the updated records back to 'studrec.dat'
Each student record is stored as a tuple in the format: (roll_number, ...)
Raises:
FileNotFoundError: If 'studrec.dat' does not exist.
pickle.UnpicklingError: If the file contains corrupted data.
ValueError: If the user input cannot be converted to an integer.
"""
try:
# Read existing student records from file
with open("studrec.dat", "rb") as file:
student_records: List[Tuple[int, ...]] = pickle.load(file)
print("Current student records:", student_records)
# Get roll number to delete
roll_number: int = int(input("Enter the roll number to delete: "))
# Filter out the record with the specified roll number
updated_records: List[Tuple[int, ...]] = [
record for record in student_records if record[0] != roll_number
]
# Write updated records back to file
with open("studrec.dat", "wb") as file:
pickle.dump(updated_records, file)
print(f"Record with roll number {roll_number} has been deleted.")
except FileNotFoundError:
print("Error: The file 'studrec.dat' does not exist.")
except pickle.UnpicklingError:
print("Error: The file contains corrupted data.")
except ValueError as e:
print(f"Error: Invalid input. Please enter an integer. {e}")
except Exception as e:
print(f"An unexpected error occurred: {e}")
if __name__ == "__main__":
delete_student_record()