Enter Update and Display Student data in binary file
The given code is for creating a simple interactive menu-based program that allows you to manage student data using a Python dictionary and binary file storage. The program provides the following functionalities:
1. Add new student data: You can input the roll number, name, and contact number of one or more students, and the data will be stored in a dictionary.
2. Update existing student data: You can update the name and contact number of a student based on their roll number.
3.View existing student data: You can view all the stored student data (roll number, name, and contact number).
4.Exit: This option allows you to exit the program.
The data is stored in a binary file named “student1.dat” using the `pickle` module, which allows for easy serialization and deserialization of Python objects. The program follows these steps:
- It opens the file in binary read and write mode.
- It loads any existing data from the file using `pickle.load()`.
- It enters an infinite loop to present the menu options to the user.
- Depending on the selected menu item, it performs the respective action:
- For adding data, it prompts you to input student details and updates the dictionary.
- For updating data, it allows you to input a roll number and update the name and contact number if the student exists.
- For viewing data, it displays the current data dictionary.
- For exiting, it breaks out of the loop and closes the file.
- It uses the `pickle.dump()` function to update the modified data back into the file.
The program is designed to handle exceptions, so if any errors occur during its execution, it will print an error message. The `finally` block ensures that the file is closed properly regardless of whether an error occurred or not.
Enter Update and Display Student data in binary file
import pickle def dicMenu(): try: fo = open("student1.dat", "rb+") # Open the file in binary read and write mode studentdata = {} try: studentdata = pickle.load(fo) # Load existing data from the file except EOFError: pass # If file is empty, ignore the error while True: print("Select the function you desire to perform:") print("1. Add new student data") print("2. Update existing student data") print("3. View existing student data") print("4. Exit") menuitem = int(input("Enter your choice: ")) if menuitem == 1: count = int(input("How many students would you like to add? ")) for i in range(count): rollno = int(input("Enter student roll number: ")) name = input("Enter student name: ") contact = input("Enter student contact number: ") studentdata[rollno] = {"Name": name, "Contact No.": contact} # Update dictionary print(studentdata) fo.seek(0) # Move the file pointer to the beginning pickle.dump(studentdata, fo) # Dump the updated data back to the file elif menuitem == 2: rollno = int(input("Enter the roll number of the student to update: ")) if rollno in studentdata: name = input("Enter updated student name: ") contact = input("Enter updated student contact number: ") studentdata[rollno]["Name"] = name studentdata[rollno]["Contact No."] = contact fo.seek(0) # Move the file pointer to the beginning pickle.dump(studentdata, fo) # Dump the updated data back to the file print("Student data updated successfully.") else: print("Student with roll number", rollno, "not found.") elif menuitem == 3: if studentdata: print(studentdata) else: print("No student data found.") elif menuitem == 4: break except Exception as e: print("An error occurred:", e) finally: fo.close() dicMenu()
Output:
Select the function you desire to perform:
1. Add new student data
2. Update existing student data
3. View existing student data
4. Exit
Enter your choice: 1
How many students would you like to add? 3
Enter student roll number: 1
Enter student name: "a"
Enter student contact number: 1
Enter student roll number: 2
Enter student name: "b"
Enter student contact number: 2
Enter student roll number: 3
Enter student name: "c"
Enter student contact number: 3
{1: {'Name': 'a', 'Contact No.': 1}, 2: {'Name': 'b', 'Contact No.': 2}, 3: {'Name': 'c', 'Contact No.': 3}}
Select the function you desire to perform:
1. Add new student data
2. Update existing student data
3. View existing student data
4. Exit
Enter your choice: 2
Enter the roll number of the student to update: 1
Enter updated student name: "d"
Enter updated student contact number: 4
Student data updated successfully.
Select the function you desire to perform:
1. Add new student data
2. Update existing student data
3. View existing student data
4. Exit
Enter your choice: 3
{1: {'Name': 'd', 'Contact No.': 4}, 2: {'Name': 'b', 'Contact No.': 2}, 3: {'Name': 'c', 'Contact No.': 3}}
Select the function you desire to perform:
1. Add new student data
2. Update existing student data
3. View existing student data
4. Exit
Enter your choice:
Output Screenshot :
Here are some questions that arise from the program Enter Update and Display Student data in binary file
1. What is the purpose of the `student.dat` file in the program?
The `student.dat` file serves as a binary file where student data is stored and retrieved. It allows the program to persistently store and manage student information.
2. Explain the purpose of the `pickle` module in this program.
The `pickle` module in the program is used for serialization and deserialization of Python objects. It allows the program to convert complex data structures, like dictionaries, into a binary format that can be written to and read from files.
3. How does the program handle existing data when you first run it?
When you first run the program, it attempts to load existing data from the `student.dat` file using `pickle.load(fo)`. If the file is empty or doesn’t exist, it catches the `EOFError` and proceeds with an empty `studentdata` dictionary.
4. Describe the main menu options available in the program.
The main menu options in the program are:
- Add new student data: Allows you to add new student records to the `studentdata` dictionary and save them to the file.
- Update existing student data: Lets you modify the name and contact information of an existing student.
- View existing student data: Displays the current student data stored in the program.
- Exit: Quits the program.
5. What happens when you choose option 1 from the main menu?
Choosing option 1 allows you to add new student data. You can specify the number of students to add, input their roll numbers, names, and contact numbers. The data is then stored in the `studentdata` dictionary and saved to the file.
6. How does the program ensure that the data is written back to the file after adding new student data?
After adding new student data, the program uses `pickle.dump(studentdata, fo)` to write the updated `studentdata` dictionary back to the `student.dat` file. This ensures that the new data is stored persistently.
7. Walk through the process of updating existing student data using option 2 in the menu.
To update existing student data using option 2, you enter the roll number of the student you want to update. If the roll number is found in the records, you can input the new name and contact number, and the program will update the information for that student in the dictionary and file.
8. What happens if you try to update the data of a student whose roll number is not in the existing records?
If you attempt to update the data of a student whose roll number is not in the existing records, the program informs you that the student is not found and the update process is not performed.
9. Explain the purpose of the `finally` block in the program.
The `finally` block in the program is used to ensure that the file (`fo`) is properly closed, regardless of whether an exception occurs or not. This helps prevent resource leaks and ensures that the file is closed when the program finishes execution or when an error occurs.
10. How does the program handle exceptions during its execution?
The program uses exception handling to catch various potential errors that could occur during execution, such as file I/O errors, input conversion errors, and `EOFError` when loading data. If an exception occurs, the program prints an error message along with the details of the exception.
11. If the `student.dat` file does not exist initially, what will happen when the program is run?
If the `student.dat` file does not exist initially, the program will catch the `EOFError` exception when attempting to load data from it. This indicates that the file is empty, and the program will proceed with an empty `studentdata` dictionary.
12. How can you ensure that the student data is correctly stored and loaded from the binary file?
To ensure correct storage and retrieval of student data, the program uses the `pickle` module’s `dump` and `load` functions to write and read data to and from the binary file. The program also appropriately handles exceptions and uses the `seek(0)` function to reset the file pointer when updating data.
13. Explain the significance of using `seek(0)` before writing data back to the file.
The `seek(0)` function is used to move the file pointer to the beginning of the file before writing data back to it. This ensures that the updated data will overwrite the existing data in the file without leaving remnants from the previous data.