Here's a simple Parking Management System implemented in
Python. It allows users to:
Input the number of vehicles entering and leaving.
Manage parking slots.
Save and update the status of slots in a CSV file.
Show slot availability.
📁 File: parking_management.py
import csv
import os
CSV_FILE = 'parking_slots.csv'
TOTAL_SLOTS = 10 # You can change this number
def initialize_slots():
if not os.path.exists(CSV_FILE):
with open(CSV_FILE, 'w', newline='') as
f:
writer = csv.writer(f)
writer.writerow(['Slot', 'Status'])
# Header
for i in range(1, TOTAL_SLOTS + 1):
writer.writerow([f'Slot{i}',
'Empty'])
def load_slots():
with open(CSV_FILE, 'r') as f:
reader = csv.DictReader(f)
return list(reader)
def save_slots(slots):
with open(CSV_FILE, 'w', newline='') as f:
writer = csv.DictWriter(f,
fieldnames=['Slot', 'Status'])
writer.writeheader()
writer.writerows(slots)
def show_available_slots(slots):
available = [slot for slot in slots if
slot['Status'] == 'Empty']
print(f"\nAvailable Slots:
{len(available)}")
for slot in available:
print(slot['Slot'])
def enter_vehicle(slots, count):
available = [slot for slot in slots if
slot['Status'] == 'Empty']
if count > len(available):
print("Not enough available slots!")
return slots
for i in range(count):
available[i]['Status'] = 'Occupied'
print(f"{count} vehicle(s) entered.")
return slots
def leave_vehicle(slots, count):
occupied = [slot for slot in slots if
slot['Status'] == 'Occupied']
if count > len(occupied):
print("Fewer vehicles in parking than
you want to remove.")
return slots
for i in range(count):
occupied[i]['Status'] = 'Empty'
print(f"{count} vehicle(s) left.")
return slots
def main():
initialize_slots()
while True:
slots = load_slots()
show_available_slots(slots)
try:
entering = int(input("\nNumber of
vehicles entering (0 to skip): "))
leaving = int(input("Number of
vehicles leaving (0 to skip): "))
except ValueError:
print("Please enter valid
numbers.")
continue
if entering < 0 or leaving < 0:
print("Negative values not
allowed.")
continue
if entering > 0:
slots = enter_vehicle(slots,
entering)
if leaving > 0:
slots = leave_vehicle(slots,
leaving)
save_slots(slots)
cont = input("\nContinue? (y/n):
").strip().lower()
if cont != 'y':
break
if __name__ == '__main__':
main()
📝 CSV File Example (after execution)
python-repl
CopyEdit
Slot,Status
Slot1,Occupied
Slot2,Empty
...
This script ensures that:
It never allocates more vehicles than the available slots.
Slot statuses are persistent between runs using a CSV file