Editor

Python Program to Store and Search User IDs and Passwords in a CSV File

Intermediate · File handling · CBSE class 12 practical list

A CSV (comma separated values) file stores a table as plain text, one row per line with commas between the values, and it opens in any spreadsheet program. This program uses Python's csv module to save user IDs and passwords in a CSV file, then searches the file for a user ID and prints its password. It is the CSV file program in the CBSE class 12 practical list.

Program

csv_user_password.py
import csv
# Make a sample file to work on (in your practical file, this file already exists)
users = [
["user_id", "password"], # the header row
["aarav01", "sun@123"],
["diya_k", "moon#456"],
["kabir7", "star!789"],
]
with open("users.csv", "w", newline="") as f:
writer = csv.writer(f)
writer.writerows(users)
print("Saved", len(users) - 1, "users in users.csv")
def find_password(user_id):
with open("users.csv", "r", newline="") as f:
reader = csv.reader(f)
next(reader) # skip the header row
for row in reader: # each row is a list: [user_id, password]
if row[0] == user_id:
print("Password for", user_id, "is", row[1])
return
print("User ID", user_id, "not found")
find_password("diya_k") # or: find_password(input("Enter user ID: "))
find_password("rohan")
Output
Saved 3 users in users.csv
Password for diya_k is moon#456
User ID rohan not found

How it works

  1. writer.writerows(users) writes each inner list as one line of the file, with commas between the values.
  2. The csv module asks for newline="" when opening the file; without it, Windows adds an extra blank line after every row.
  3. csv.reader(f) gives back each line as a list of strings, and next(reader) skips the header row.
  4. When row[0] matches the user ID, the password in row[1] is printed and return ends the search; if the loop finishes without a match, the not found message is printed.
  5. Real websites should never store passwords as plain text like this (they store a scrambled form called a hash), but plain text is fine for practising file handling.

Another way to write it

Using csv.DictReader

Using csv.DictReader
import csv
# Make a sample file to work on (in your practical file, this file already exists)
with open("users.csv", "w", newline="") as f:
writer = csv.writer(f)
writer.writerows([["user_id", "password"], ["aarav01", "sun@123"], ["diya_k", "moon#456"]])
user_id = "aarav01"
with open("users.csv", "r", newline="") as f:
for row in csv.DictReader(f): # each row is a dictionary keyed by the header
if row["user_id"] == user_id:
print("Password for", user_id, "is", row["password"])
break
else:
print("User ID", user_id, "not found")
Output
Password for aarav01 is sun@123

csv.DictReader reads the header row by itself and gives each row as a dictionary, so you write row["password"] instead of remembering that the password is row[1].

Try it yourself

Write a function that takes a new user ID and password and adds them to the file by opening it with "a" (append) instead of "w".

Edit the program above and press Run, or open it in the full editor to save it and share it. To watch it run line by line, open it in the visualizer.

All Python programs · CBSE class 12 practical file · Python exercises