0% found this document useful (0 votes)
16 views5 pages

Hosptal Management Program

Project on hospital management

Uploaded by

shaiknoor1234
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)
16 views5 pages

Hosptal Management Program

Project on hospital management

Uploaded by

shaiknoor1234
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
You are on page 1/ 5

import csv

# File to store patient records


FILE_NAME = "patients.csv"

# Function to initialize the file


def initialize_file():
try:
with open(FILE_NAME, 'x', newline='') as file:
writer = csv.writer(file)
writer.writerow(["ID", "Name", "Age", "Gender", "Ailment", "Doctor",
"Contact"])
except FileExistsError:
pass

# Function to add a new patient


def add_patient():
with open(FILE_NAME, 'a', newline='') as file:
writer = csv.writer(file)
patient_id = input("Enter Patient ID: ")
name = input("Enter Patient Name: ")
age = input("Enter Age: ")
gender = input("Enter Gender (M/F): ")
ailment = input("Enter Ailment: ")
doctor = input("Enter Doctor Assigned: ")
contact = input("Enter Contact Number: ")
writer.writerow([patient_id, name, age, gender, ailment, doctor, contact])
print("Patient added successfully!\n")

# Function to search for a patient


def search_patient():
patient_id = input("Enter Patient ID to search: ")
found = False
with open(FILE_NAME, 'r') as file:
reader = csv.reader(file)
next(reader) # Skip header row
for row in reader:
if row[0] == patient_id:
print("Patient Details:")
print(f"ID: {row[0]}, Name: {row[1]}, Age: {row[2]}, Gender: {row[3]},
Ailment: {row[4]}, Doctor: {row[5]}, Contact: {row[6]}")
found = True
break
if not found:
print("Patient not found!\n")

# Function to update patient details


def update_patient():
patient_id = input("Enter Patient ID to update: ")
updated = False
rows = []
with open(FILE_NAME, 'r') as file:
reader = csv.reader(file)
rows = list(reader)
with open(FILE_NAME, 'w', newline='') as file:
writer = csv.writer(file)
for row in rows:
if row[0] == patient_id:
print("Enter new details (leave blank to keep unchanged):")
row[1] = input(f"Name ({row[1]}): ") or row[1]
row[2] = input(f"Age ({row[2]}): ") or row[2]
row[3] = input(f"Gender ({row[3]}): ") or row[3]
row[4] = input(f"Ailment ({row[4]}): ") or row[4]
row[5] = input(f"Doctor ({row[5]}): ") or row[5]
row[6] = input(f"Contact ({row[6]}): ") or row[6]
updated = True
writer.writerow(row)
if updated:
print("Patient details updated successfully!\n")
else:
print("Patient not found!\n")

# Function to delete a patient


def delete_patient():
patient_id = input("Enter Patient ID to delete: ")
deleted = False
rows = []
with open(FILE_NAME, 'r') as file:
reader = csv.reader(file)
rows = list(reader)

with open(FILE_NAME, 'w', newline='') as file:


writer = csv.writer(file)
for row in rows:
if row[0] == patient_id:
deleted = True
continue
writer.writerow(row)
if deleted:
print("Patient record deleted successfully!\n")
else:
print("Patient not found!\n")

# Function to view all patients


def view_all_patients():
with open(FILE_NAME, 'r') as file:
reader = csv.reader(file)
for row in reader:
print(", ".join(row))

# Main menu
def main_menu():
initialize_file()
while True:
print("====== Hospital Management System ======")
print("1. Add Patient")
print("2. Search Patient")
print("3. Update Patient Details")
print("4. Delete Patient")
print("5. View All Patients")
print("6. Exit")
choice = input("Enter your choice: ")

if choice == '1':
add_patient()
elif choice == '2':
search_patient()
elif choice == '3':
update_patient()
elif choice == '4':
delete_patient()
elif choice == '5':
view_all_patients()
elif choice == '6':
print("Exiting the program. Goodbye!")
break
else:
print("Invalid choice. Please try again.\n")

if __name__ == "__main__":
main_menu()

You might also like