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

Student Record Manager in Python

It's a mini project of python on students record regarding... it's important or enough in order to understand the basics of python...

Uploaded by

studytent1700
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)
9 views4 pages

Student Record Manager in Python

It's a mini project of python on students record regarding... it's important or enough in order to understand the basics of python...

Uploaded by

studytent1700
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

# Mini Project Title: Student Record File Manager in Python

# Objective:
# Create a mini project that manages student records using file handling. This pro

# Project Description: Build a command-line tool where a user (admin) can:

# 1. Add student records


# 2. View student records
# 3. Search for a student by roll number
# 4. Delete a student record
# 5. Store records in a file for persistent storage

import sys

class EmptyInputError(Exception):
...

try:
with open("student_records.txt", "r") as file:
pass
except FileNotFoundError:
print("File not found. Create the file first...")
choice = input("Do you want to create a file or not? (yes/no): ")
if choice == "yes":
with open("student_records.txt", "a") as file:
print("File successfully created....")
else:
[Link]()

def add_student_record():
with open("student_records.txt", "a") as file:
try:
name = input("Enter the name: ")
roll = input("Enter the roll number: ")
cgpa = input("Enter the cgpa: ")

if not name or not roll or not cgpa:


raise EmptyInputError("Fields cannot be empty.")
else:
[Link](f"name: {name} roll: {roll} cgpa: {cgpa}\n")
except EmptyInputError as e:
print("Error", e)

def view_student_records():
with open("student_records.txt", "r") as file:
records = [Link]()
for record in records:
print(record)

def delete_student_record():
with open("student_records.txt", "r") as file:

records = [Link]()
for record in records:
spark
print("Existing roll numbers: ")

attributes = [Link]().split()
roll_index = [Link]('roll:') + 1
print(attributes[roll_index])

roll_to_delete = input("Enter the roll number that you want to delete: ")
updated_records = []
for record in records:
if f"roll: {roll_to_delete}" not in record:
updated_records.append(record)

with open("student_records.txt", "w") as file:


[Link](updated_records)

print(f"Record with roll number {roll_to_delete} has been deleted!")

def search_student_record():
with open("student_records.txt", "r") as file:
records = [Link]()

roll_to_search = input("Enter the roll number that you want to search: ")
print("The record for a searched roll number is: ")
for record in records:
if f"roll: {roll_to_search}" in record:
print(record)

def view_file_attribute():
with open("student_records.txt", "r") as file:
print("file name: ", [Link])
print("file mode: ", [Link])
print("is file closed? : ", [Link])
print("file encoding: ", [Link])
print("is file readable? ", [Link]())
print("Is file writable? ", [Link]())
[Link]()

while True:
print("1. Add Student Record ")
print("2. View the student records")
print("3. Delete the student record")
print("4. View the file attributes")
print("5. Search the student record")
print("9. Exit the program")

choice = input("Enter your choice: ")

if(choice == "1"):
add_student_record()
elif choice == "2":
view_student_records()
elif choice == "3":
delete_student_record()
elif choice == "4":
view_file_attribute()
elif choice == "5":
search_student_record()
elif choice == "9":
break
File does ot exist. Please create the file first...
Do you want to create the file? (yes/no): no
ERROR:root:Internal Python error in the inspect module.
Below is the traceback from this internal error.

Exiting the program...


Traceback (most recent call last):
File "/tmp/[Link]", line 4, in <cell line: 0>
file = open("student_records.txt", "r")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: 'student_records.tx

During handling of the above exception, another exception occurred:

Traceback (most recent call last):


File "/usr/local/lib/python3.12/dist-packages/IPython/core/interactiveshe
exec(code_obj, self.user_global_ns, self.user_ns)
File "/tmp/[Link]", line 14, in <cell line: 0>
[Link]()
SystemExit

During handling of the above exception, another exception occurred:

Traceback (most recent call last):


File "/usr/local/lib/python3.12/dist-packages/IPython/core/[Link]", l
return _fixed_getinnerframes(etb, number_of_lines_of_context, tb_offset
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/dist-packages/IPython/core/[Link]", l
return f(*args, **kwargs)
^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/dist-packages/IPython/core/[Link]", l
records = fix_frame_records_filenames([Link](etb, conte
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.12/[Link]", line 1769, in getinnerframes
traceback_info = getframeinfo(tb, context)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.12/[Link]", line 1701, in getframeinfo
lineno = frame.f_lineno
^^^^^^^^^^^^^^
AttributeError: 'tuple' object has no attribute 'f_lineno'
--------------------------------------------------------------------------
-
FileNotFoundError Traceback (most recent call
last)
/tmp/[Link] in <cell line: 0>()
3 try:
----> 4 file = open("student_records.txt", "r")
5 [Link]()

FileNotFoundError: [Errno 2] No such file or directory:

Common questions

Powered by AI

The program uses exception handling by capturing specific errors such as FileNotFoundError and EmptyInputError to prompt the user with meaningful messages and actions. This improves user experience by preventing abrupt program crashes and providing guidance on corrective actions. However, limitations exist as not all potential errors are caught, leading to unexpected program terminations from unhandled exceptions. Adequate error logging and handling should be expanded to cover more scenarios to fully protect the application from unexpected disruptions .

The program faces the challenge of encountering an unhandled rollback error when handling a FileNotFoundError. The traceback suggests issues in the Python internal handling mechanisms, possibly due to improper cleanup or error management in the script. It attempts to address the FileNotFoundError by prompting the user to decide whether to create the missing file. However, additional error handling and logging is needed to comprehensively capture and address all related exceptions to avoid unexpected errors such as 'AttributeError: 'tuple' object has no attribute 'f_lineno'' .

The program can be terminated by the user selecting the option to exit the program from the menu or by encountering a FileNotFoundError if the 'student_records.txt' file does not exist. Upon such a file-related error, the program prompts the user whether they would like to create the file. If the user opts not to create the file, the program uses 'sys.exit()' to terminate properly, ensuring that any unhandled exceptions do not disrupt the functionality .

During the deletion of a student record, the program first reads all the records from 'student_records.txt' into memory and then iterates over these records to find the roll number specified for deletion. It constructs a list of updated records excluding the matched roll number. Finally, it opens 'student_records.txt' in write mode and writes this updated list back to the file, thus removing the target record. This method ensures data integrity by preserving all records except the one marked for deletion, thus maintaining consistent and accurate data storage .

The search functionality is implemented by reading all records from 'student_records.txt', then iterating through them to find a match for the specified roll number. Upon finding the match, the corresponding record is printed. For efficiency improvement, the program could index the file by roll numbers, creating a dictionary or using a database system that allows faster lookup times compared to linear searching through the entire file each time a search is performed .

The primary objective of the Student Record File Manager project in Python is to manage student records using file handling. It achieves persistent storage of records by writing the student details such as name, roll number, and CGPA into a text file named 'student_records.txt'. This allows the records to persist beyond the execution of the program, enabling users to add, view, search, and delete student records as needed .

When adding a new student record, the program performs input validation by checking if any of the input fields (name, roll number, CGPA) are empty. If any field is left unfilled, the program raises a custom exception named 'EmptyInputError'. This exception is specifically designed to handle situations where input fields are left empty, ensuring that all necessary data is provided before writing to the file .

The menu-driven user interaction in the program is structured linearly, providing clear options for the user to select actions such as adding, viewing, deleting records, or exiting the program. While functional, the interaction could be improved through more user-friendly prompts, input validation for choice selections, and navigation shortcuts. Additionally, integrating a graphical user interface (GUI) could enhance accessibility and provide a more intuitive user experience over a command-line interface .

The 'view_file_attribute' function displays several key attributes of the file 'student_records.txt', including the file name, file mode, whether the file is closed, the file encoding, and the readability and writability of the file. These attributes are important in a file management application because they provide essential metadata about the file's current status and capabilities, which can inform decisions on how the file can be used or manipulated within the application .

Storing student records in a plain text file poses significant security risks. Text files are easily accessible and lack encryption, exposing sensitive student information to unauthorized access. If this file is stored on a shared system or transferred over insecure connections, the data can be compromised. To mitigate these risks, encryption protocols should be implemented, either by encrypting the file itself or, ideally, by transitioning to a secure database system with access controls and logging .

You might also like