Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Birthday Reminder Application in Python
In this tutorial, we will create a birthday reminder application using Python that checks for birthdays on the current date and sends system notifications.
Problem Statement
Create a Python application that checks whether there is any birthday on the current day. If it's someone's birthday from our list, send a system notification with their name.
We need a lookup file to store dates and names. Create a text file named birth_day_lookup.txt with this format ?
Implementation Steps
- Read the birthday lookup file
- Check if current date matches any entry
- Send system notification for matching birthdays
Python Code
import os
import time
# Take the birthday lookup file from home directory
file_path = os.getenv('HOME') + '/birth_day_lookup.txt'
def check_birthday():
try:
lookup_file = open(file_path, 'r') # open the lookup file as read mode
today = time.strftime('%d-%B') # get today's date as dd-Month format
bday_flag = 0
birthday_names = []
# loop through each entry in the birthday file
for entry in lookup_file:
if today in entry:
line = entry.split(' ') # split the line on spaces to get name and surname
if len(line) >= 3: # ensure we have date, first name, and last name
bday_flag = 1
full_name = line[1] + ' ' + line[2].strip()
birthday_names.append(full_name)
if bday_flag == 1:
names_str = ', '.join(birthday_names)
os.system('notify-send "Birthday Today!" "Today is ' + names_str + ''s Birthday"')
else:
os.system('notify-send "No Birthdays" "No birthday for today is listed"')
lookup_file.close()
except FileNotFoundError:
os.system('notify-send "Error" "Birthday lookup file not found"')
except Exception as e:
os.system('notify-send "Error" "Failed to check birthdays"')
# Run the birthday check
check_birthday()
Creating the Lookup File
First, create the birthday file in your home directory ?
import os
# Create sample birthday file
file_path = os.path.expanduser('~/birth_day_lookup.txt')
sample_data = """10-January John Doe
15-March Jane Smith
22-December Alice Johnson
05-July Bob Wilson"""
with open(file_path, 'w') as f:
f.write(sample_data)
print(f"Birthday file created at: {file_path}")
Birthday file created at: /home/user/birth_day_lookup.txt
Testing the Application
To test with today's date, temporarily add an entry with the current date ?
import time
# Get today's date in the required format
today = time.strftime('%d-%B')
print(f"Today's date format: {today}")
print("Add this to your lookup file to test:")
print(f"{today} Test Person")
Today's date format: 15-December Add this to your lookup file to test: 15-December Test Person
Setting Up as Startup Application
Step 1: Make Script Executable
sudo chmod +x birthday_reminder.py
Step 2: Move to System Directory
sudo cp birthday_reminder.py /usr/bin/
Step 3: Add to Startup Applications
Search for "Startup Applications" in your system and add the script with command: /usr/bin/birthday_reminder.py
Enhanced Version with Logging
import os
import time
import logging
# Setup logging
logging.basicConfig(
filename=os.path.expanduser('~/birthday_reminder.log'),
level=logging.INFO,
format='%(asctime)s - %(message)s'
)
def check_birthday():
file_path = os.path.expanduser('~/birth_day_lookup.txt')
today = time.strftime('%d-%B')
try:
with open(file_path, 'r') as lookup_file:
birthday_people = []
for entry in lookup_file:
if today in entry.strip():
parts = entry.strip().split(' ')
if len(parts) >= 3:
name = ' '.join(parts[1:])
birthday_people.append(name)
if birthday_people:
names = ', '.join(birthday_people)
message = f"Today is {names}'s Birthday!"
os.system(f'notify-send "Birthday Reminder" "{message}"')
logging.info(f"Birthday notification sent for: {names}")
else:
logging.info("No birthdays today")
except Exception as e:
logging.error(f"Error checking birthdays: {e}")
check_birthday()
Conclusion
This birthday reminder application automatically checks for birthdays daily and sends system notifications. You can enhance it further by adding email notifications, GUI interface, or integration with calendar applications for more comprehensive birthday management.
