Back to Tutorials
Python

Student Management System

Build a fully functional console-based Student Management System in Python that stores, reads, updates, and deletes student records permanently using file handling - no database needed.

~45 mins Intermediate Python 3 File Handling
Skills & Concepts You'll Gain
Tech / Tools Used: Python 3, CSV file format, Python IDLE / VS Code / any terminal
What You Need
  • Python 3 (3.7 or later recommended)
  • A text editor or IDE
  • A terminal / command prompt
  • No external libraries needed
Recommended IDE
  • VS Code (free, beginner friendly)
  • Python IDLE (comes with Python)
  • PyCharm Community Edition
  • Thonny (great for beginners)
Step 1

Setup - Install Python and Prepare Your Environment

Before we write any code, make sure Python is installed on your computer. If you already have it, you can skip ahead to the next step.

1.1 Install Python

  1. Go to python.org/downloads in your browser.
  2. Download the latest version of Python 3 (for example, Python 3.12).
  3. Run the installer. Important: On Windows, check the box that says "Add Python to PATH" before clicking Install Now. If you skip this step, Python won't work from your terminal.
  4. After installation, open a terminal (Command Prompt on Windows, Terminal on Mac/Linux) and type:
    Terminal
    python --version
    You should see something like Python 3.12.0. If you see a version number, you're all set!

1.2 Install VS Code (Recommended)

  1. Go to code.visualstudio.com and download VS Code for your operating system.
  2. Install it, then open it.
  3. Inside VS Code, go to the Extensions tab (left sidebar, icon that looks like four squares).
  4. Search for "Python" and install the extension by Microsoft. This gives you syntax highlighting and code suggestions.

1.3 Create Your Project Folder

  1. Create a new folder on your computer - name it student_management.
  2. Open this folder in VS Code: File → Open Folder.
  3. Create a new file inside it called student_management.py.
  4. Also create a file called students.csv - leave it empty for now. This will be our data file.
We are using a CSV file (Comma-Separated Values) as our "database". A CSV file is just a plain text file that stores data in rows and columns, separated by commas. It can also be opened in Microsoft Excel!
This project uses zero external libraries - only Python's built-in csv and os modules. No pip install needed!
Step 2

Understand the Structure - What Each Part Does

Before jumping into code, it is very important to understand what our program will do and how it is broken into small, manageable functions. Think of each function as a small machine that does one specific job.

Our Student Management System will support these operations - often called CRUD:


Function Breakdown

Here is every function we will write, explained in plain English before you see the code:

Function Name What It Does
initialize_file() Creates the CSV file with a header row (Name, Roll No, Grade, Marks) if it doesn't already exist. Runs once when the program starts.
add_student() Asks the user for a student's details and appends a new row to the CSV file. Uses append mode so existing records are not erased.
view_all_students() Opens the CSV file, reads every row, and prints each student's details to the console in a neat, readable format.
search_student() Asks for a Roll Number, scans through all records, and displays the matching student's details if found.
update_student() Finds a student by Roll Number, asks for new values, rewrites the entire file with the updated record. This is called a rewrite technique.
delete_student() Finds a student by Roll Number, removes that record, then rewrites the file without that row.
main_menu() Displays a numbered menu and runs in a loop, calling the correct function based on what the user types. This is the heart of the program.
Why CSV? CSV is one of the simplest and most universal formats to store data. Every row is a student record. Every value is separated by a comma. Python has a built-in csv module that makes reading and writing these files very easy - no complicated setup needed.

The CSV File Format We'll Use

Our students.csv file will look like this after a few students are added:

Name,Roll No,Grade,Marks Aarav Sharma,101,10th,87.5 Priya Patel,102,11th,91.0 Rohan Mehta,103,10th,74.0

The first row is the header (column names). Each row after that is one student's record.

Step 3

Write the Full Code

Copy the complete code below into your student_management.py file. Read the comments (lines starting with #) - they explain what each block does as you go.

student_management.py
# ============================================
# Student Management System
# NKDevSpace Tutorial
# Uses Python File Handling (CSV Format)
# ============================================

import csv
import os

# ----- CONFIGURATION -----
FILE_NAME = "students.csv"
HEADERS   = ["Name", "Roll No", "Grade", "Marks"]


# ============================================
# 1. INITIALIZE - Create file if not present
# ============================================
def initialize_file():
    """Creates the CSV file with headers if it doesn't exist."""
    if not os.path.exists(FILE_NAME):
        with open(FILE_NAME, mode="w", newline="") as file:
            writer = csv.writer(file)
            writer.writerow(HEADERS)
        print(f"[INFO] File '{FILE_NAME}' created successfully.\n")


# ============================================
# 2. ADD - Append a new student record
# ============================================
def add_student():
    print("\n--- Add New Student ---")
    name    = input("Enter Student Name   : ").strip()
    roll_no = input("Enter Roll Number    : ").strip()
    grade   = input("Enter Grade/Class    : ").strip()
    marks   = input("Enter Marks (0-100)  : ").strip()

    # Basic validation - make sure no field is left empty
    if not name or not roll_no or not grade or not marks:
        print("[ERROR] All fields are required. Student not added.\n")
        return

    # Append mode ("a") adds to the end without erasing existing data
    with open(FILE_NAME, mode="a", newline="") as file:
        writer = csv.writer(file)
        writer.writerow([name, roll_no, grade, marks])

    print(f"\n[SUCCESS] Student '{name}' added successfully!\n")


# ============================================
# 3. VIEW - Display all student records
# ============================================
def view_all_students():
    print("\n--- All Students ---")

    with open(FILE_NAME, mode="r") as file:
        reader = csv.DictReader(file)
        students = list(reader)

    if not students:
        print("[INFO] No student records found.\n")
        return

    # Print table header
    print(f"\n{'#':<5} {'Name':<20} {'Roll No':<10} {'Grade':<10} {'Marks':<8}")
    print("-" * 58)

    # Print each student row
    for index, student in enumerate(students, start=1):
        print(
            f"{index:<5} {student['Name']:<20} {student['Roll No']:<10} "
            f"{student['Grade']:<10} {student['Marks']:<8}"
        )

    print(f"\n[INFO] Total students: {len(students)}\n")


# ============================================
# 4. SEARCH - Find a student by Roll Number
# ============================================
def search_student():
    print("\n--- Search Student ---")
    roll_no = input("Enter Roll Number to search: ").strip()

    with open(FILE_NAME, mode="r") as file:
        reader = csv.DictReader(file)
        for student in reader:
            if student["Roll No"] == roll_no:
                print("\n[FOUND] Student Details:")
                print(f"  Name    : {student['Name']}")
                print(f"  Roll No : {student['Roll No']}")
                print(f"  Grade   : {student['Grade']}")
                print(f"  Marks   : {student['Marks']}\n")
                return

    print(f"[NOT FOUND] No student with Roll Number '{roll_no}' exists.\n")


# ============================================
# 5. UPDATE - Edit a student's record
# ============================================
def update_student():
    print("\n--- Update Student Record ---")
    roll_no = input("Enter Roll Number of student to update: ").strip()

    updated   = False
    all_rows  = []

    with open(FILE_NAME, mode="r") as file:
        reader = csv.DictReader(file)
        for student in reader:
            if student["Roll No"] == roll_no:
                print(f"\n[FOUND] Updating record for: {student['Name']}")
                print("(Press Enter to keep the current value)\n")

                new_name  = input(f"  New Name  [{student['Name']}]  : ").strip()
                new_grade = input(f"  New Grade [{student['Grade']}] : ").strip()
                new_marks = input(f"  New Marks [{student['Marks']}] : ").strip()

                # Keep old value if user pressed Enter without typing
                student["Name"]  = new_name  if new_name  else student["Name"]
                student["Grade"] = new_grade if new_grade else student["Grade"]
                student["Marks"] = new_marks if new_marks else student["Marks"]

                updated = True
            all_rows.append(student)

    if not updated:
        print(f"[NOT FOUND] No student with Roll Number '{roll_no}' exists.\n")
        return

    # Rewrite the entire file with the updated row
    with open(FILE_NAME, mode="w", newline="") as file:
        writer = csv.DictWriter(file, fieldnames=HEADERS)
        writer.writeheader()
        writer.writerows(all_rows)

    print(f"\n[SUCCESS] Student record updated successfully!\n")


# ============================================
# 6. DELETE - Remove a student by Roll Number
# ============================================
def delete_student():
    print("\n--- Delete Student ---")
    roll_no = input("Enter Roll Number of student to delete: ").strip()

    deleted  = False
    all_rows = []

    with open(FILE_NAME, mode="r") as file:
        reader = csv.DictReader(file)
        for student in reader:
            if student["Roll No"] == roll_no:
                confirm = input(
                    f"  Are you sure you want to delete '{student['Name']}'? (yes/no): "
                ).strip().lower()
                if confirm == "yes":
                    deleted = True
                    continue  # Skip this row - don't add it to all_rows
            all_rows.append(student)

    if deleted:
        # Rewrite the file without the deleted student
        with open(FILE_NAME, mode="w", newline="") as file:
            writer = csv.DictWriter(file, fieldnames=HEADERS)
            writer.writeheader()
            writer.writerows(all_rows)
        print(f"\n[SUCCESS] Student deleted successfully!\n")
    else:
        print(f"[INFO] No record was deleted.\n")


# ============================================
# 7. MAIN MENU - The program's control center
# ============================================
def main_menu():
    initialize_file()

    while True:
        print("=" * 42)
        print("   STUDENT MANAGEMENT SYSTEM")
        print("   NKDevSpace")
        print("=" * 42)
        print("  1. Add Student")
        print("  2. View All Students")
        print("  3. Search Student")
        print("  4. Update Student Record")
        print("  5. Delete Student")
        print("  6. Exit")
        print("=" * 42)

        choice = input("  Enter your choice (1-6): ").strip()

        if   choice == "1": add_student()
        elif choice == "2": view_all_students()
        elif choice == "3": search_student()
        elif choice == "4": update_student()
        elif choice == "5": delete_student()
        elif choice == "6":
            print("\n[GOODBYE] Thank you for using NKDevSpace Student Manager!\n")
            break
        else:
            print("[ERROR] Invalid choice. Please enter a number between 1 and 6.\n")


# ============================================
# ENTRY POINT
# ============================================
if __name__ == "__main__":
    main_menu()
Step 4

Code Explanation - Understanding Every Important Concept

This is the most important step. We will go through every key concept in this program in detail. Do not skip this - understanding these ideas will help you write your own projects later.


4.1 Importing Modules

import csv
import os
We import two built-in Python modules:

csv - Python's built-in module for reading and writing CSV files. Instead of manually splitting lines by commas (which can break when a value contains a comma), the csv module handles all of that safely for you.

os - Gives us access to operating system features. We use os.path.exists() to check if our data file already exists before trying to create it. This prevents accidentally overwriting existing data.

4.2 Constants (Configuration at the Top)

FILE_NAME = "students.csv"
HEADERS = ["Name", "Roll No", "Grade", "Marks"]
Instead of writing "students.csv" in every single function, we define it once at the top as a constant (a variable that never changes). This is a professional practice - if you ever want to rename your file, you only change it in one place, not in 10 places throughout the code.

HEADERS is a Python list containing the column names for our CSV file. Using ALL_CAPS names for constants is a widely accepted Python convention.

4.3 File Handling: Open Modes Explained

File handling in Python uses the open() function. The mode parameter controls what you can do with the file:

open(FILE_NAME, mode="w", newline="")
"w" = Write mode. Creates the file if it doesn't exist. If the file already exists, it erases all content and starts fresh. We use this in initialize_file() (only when the file doesn't exist yet) and when rewriting the file after an update or delete.

newline="" is important on Windows - it prevents Python from adding extra blank lines between rows in the CSV file.
open(FILE_NAME, mode="a", newline="")
"a" = Append mode. Opens the file and moves the cursor to the end. Anything you write is added after the existing content. Existing data is never erased. This is exactly what we need in add_student() - we want to add one new row at a time without touching the old ones.
open(FILE_NAME, mode="r")
"r" = Read mode. Opens the file just for reading. You cannot write to it in this mode. We use this in view_all_students(), search_student(), and at the start of update_student() and delete_student().
Always use the with open(...) as file: pattern. This automatically closes the file when the block finishes - even if an error occurs. Never forget to close files; open files can cause data corruption or memory leaks.

4.4 CSV Writer vs DictWriter

writer = csv.writer(file)
writer.writerow([name, roll_no, grade, marks])
csv.writer writes data as a plain list. Each element in the list becomes one cell in the CSV row. We use this in add_student() because we are building the row from scratch with new user input.
writer = csv.DictWriter(file, fieldnames=HEADERS)
writer.writeheader()
writer.writerows(all_rows)
csv.DictWriter writes data from a dictionary (where each key matches a column header). This is more reliable when rewriting the entire file because it ensures columns always line up correctly with the headers. We use this in update_student() and delete_student().

writeheader() writes the column names as the first row. writerows() writes all the student dictionaries at once.

4.5 CSV Reader vs DictReader

reader = csv.DictReader(file)
for student in reader:
    print(student["Name"])
csv.DictReader reads each row as a dictionary, where the keys are the column headers. So instead of accessing a student's name by position (like row[0]), you access it by name (student["Name"]). This makes the code far more readable and less error-prone.

For example, if the CSV file has this row: Aarav Sharma,101,10th,87.5, then student["Name"] gives you "Aarav Sharma" and student["Marks"] gives you "87.5".

4.6 The Update & Delete Technique (Rewrite the File)

This is the trickiest concept in file handling. CSV files don't support editing individual lines in the middle of the file. So to update or delete a record, we use this 3-step technique:

# Step 1: Read ALL rows into memory
all_rows = list(reader)

# Step 2: Modify or remove the target row
for row in all_rows: ...

# Step 3: Rewrite the file with the changes
writer.writerows(all_rows)
Analogy: Imagine you have a notebook with 10 lines of text and you want to erase line 5. You can't "cut out" just that line. Instead, you copy all 10 lines onto a new notebook, skip line 5, and throw away the old notebook. That's exactly what we do here - we read everything into memory, make our change, and rewrite the whole file.

This is the standard approach for CSV file editing in Python. The list(reader) call converts the reader object into a regular Python list so we can loop through it, modify it, and use it again.

4.7 The While Loop (Main Menu)

while True:
    # show menu
    choice = input(...)
    if choice == "6": break
while True creates an infinite loop - the menu keeps showing forever. The only way out is break, which we trigger when the user chooses option 6 (Exit). This pattern is standard for menu-driven console programs. Every iteration of the loop shows the menu, waits for input, and calls the right function.

4.8 if __name__ == "__main__"

if __name__ == "__main__":
    main_menu()
This is a Python best practice. __name__ is a special variable Python sets automatically. When you run the file directly (e.g., python student_management.py), Python sets __name__ to "__main__", so main_menu() runs.

If another Python file imports this file as a module in the future, __name__ will be the module name instead, so main_menu() will not run automatically. This keeps the program flexible for reuse.

4.9 Input Validation

name = input("Enter Name: ").strip()
if not name: return
.strip() removes any leading or trailing spaces the user might accidentally type. For example, if the user types " Aarav ", .strip() converts it to "Aarav".

The if not name check catches empty inputs. If the user just presses Enter without typing anything, the field will be an empty string and we stop the function early using return. This prevents storing blank records in our file.
Step 5

Run the Program - Step-by-Step Guide

Now that the code is written, let's run it and test all the features.

5.1 Run from VS Code

  1. Open your student_management.py file in VS Code.
  2. Press Ctrl + ` (backtick) to open the built-in terminal.
  3. Make sure you are in the right folder. Type ls (Mac/Linux) or dir (Windows) - you should see student_management.py listed.
  4. Run the program by typing:
    Terminal
    python student_management.py

5.2 Expected Output When You Start

[INFO] File 'students.csv' created successfully. ========================================== STUDENT MANAGEMENT SYSTEM NKDevSpace ========================================== 1. Add Student 2. View All Students 3. Search Student 4. Update Student Record 5. Delete Student 6. Exit ========================================== Enter your choice (1-6):

5.3 Test: Add a Student (Option 1)

Type 1 and press Enter. You'll be prompted:

--- Add New Student --- Enter Student Name : Aarav Sharma Enter Roll Number : 101 Enter Grade/Class : 10th Enter Marks (0-100) : 87.5 [SUCCESS] Student 'Aarav Sharma' added successfully!

Add 2–3 more students. Then try option 2 to view them all.

5.4 - Test: View All Students (Option 2)

--- All Students --- # Name Roll No Grade Marks ---------------------------------------------------------- 1 Aarav Sharma 101 10th 87.5 2 Priya Patel 102 11th 91.0 3 Rohan Mehta 103 10th 74.0 [INFO] Total students: 3

5.5 - Test: Update a Student (Option 4)

Type 4, enter Roll Number 101, update the marks, and press Enter for fields you don't want to change.

[FOUND] Updating record for: Aarav Sharma (Press Enter to keep the current value) New Name [Aarav Sharma] : New Grade [10th] : New Marks [87.5] : 92.0 [SUCCESS] Student record updated successfully!

5.6 Check Your CSV File

Open students.csv in VS Code or Excel. You'll see all your students saved there permanently. Close the program and run it again - all your data will still be there. That's the power of file handling!

If you see a FileNotFoundError, make sure your terminal is in the same folder as your student_management.py file. Use cd folder_name to navigate into the correct folder first.
Step 6

Try It Yourself - Challenges to Level Up

You've built the core system. Now push further with these challenges and turn a good project into a great one:

Next Tutorial