# 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:
sys.exit()
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:
file.write(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 = file.readlines()
for record in records:
print(record)
def delete_student_record():
with open("student_records.txt", "r") as file:
records = file.readlines()
for record in records:
spark
print("Existing roll numbers: ")
attributes = record.strip().split()
roll_index = attributes.index('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:
file.writelines(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 = file.readlines()
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: ", file.name)
print("file mode: ", file.mode)
print("is file closed? : ", file.closed)
print("file encoding: ", file.encoding)
print("is file readable? ", file.readable())
print("Is file writable? ", file.writable())
file.close()
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/ipython-input-2780520932.py", 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/ipython-input-2780520932.py", line 14, in <cell line: 0>
sys.exit()
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/ultratb.py", l
return _fixed_getinnerframes(etb, number_of_lines_of_context, tb_offset
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/dist-packages/IPython/core/ultratb.py", l
return f(*args, **kwargs)
^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/dist-packages/IPython/core/ultratb.py", l
records = fix_frame_records_filenames(inspect.getinnerframes(etb, conte
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.12/inspect.py", line 1769, in getinnerframes
traceback_info = getframeinfo(tb, context)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.12/inspect.py", line 1701, in getframeinfo
lineno = frame.f_lineno
^^^^^^^^^^^^^^
AttributeError: 'tuple' object has no attribute 'f_lineno'
--------------------------------------------------------------------------
-
FileNotFoundError Traceback (most recent call
last)
/tmp/ipython-input-2780520932.py in <cell line: 0>()
3 try:
----> 4 file = open("student_records.txt", "r")
5 file.close()
FileNotFoundError: [Errno 2] No such file or directory: