One part of my program requires that the user enters a date and this date is then checked against each product in the dictionary to see if the date the product arrived plus its shelf life causes the product to expire before or after the date entered by the user.

import sys
from string import *
import pickle
import datetime

cheeseDictionary = {}
userInput = ""

def loadProduct(fileName):
    global cheeseDictionary
    f = open(fileName,"r")
    line = f.readline()         # Reads line from file
    while line:
        line = line[:-1]
        data = split(line,":")  # Splits line when there is a colon
        cheeseDictionary[data[0]] = {"date":data[1], "life":data[2], "name":data[3]} # Stores each split item
        line = f.readline()     # Next line

def saveProduct(fileName,cheeseDictionary): 
    f = open(fileName, "w")
    for i in sorted(cheeseDictionary.keys()):
        v = cheeseDictionary[i]
        f.write("%s:%s:%s:%s\n" % (i, v["date"], v["life"], v["name"]))

def printProduct(cheeseDictionary):
    print "ID"," ","Date"," ","Life(days)"," ","Name"
    for cheese in cheeseDictionary:
        print cheese," ",cheeseDictionary[cheese]["date"]," ",cheeseDictionary[cheese]["life"],"    ",cheeseDictionary[cheese]["name"]
def addProduct():
    global cheeseDicitonary
    correct = 0
    idInput = ""
    dateInput = ""
    lifeInput = ""
    nameinput = ""

    while correct != 1:
        idInput = raw_input("Please enter the ID of the cheese to be added. ")
        if cheeseDictionary.has_key(idInput):
            print ("This ID already exists. Please try again.")
            correct = 0
            newID = idInput
            correct = 1
    dateInput = raw_input("Please enter the date of the cheese to be added in the format dd/mm/yyyy. ")
    lifeInput = raw_input("Please enter the life of the cheese to be added in days. ")
    nameInput = raw_input("Please enter the name of the cheese to be added. ")
    cheeseDictionary[idInput] = {"date":dateInput, "life":lifeInput, "name":nameInput}
def checkProduct(cheeseDictionary):
    dateCheck = raw_input("Please enter the date in the format dd/mm/yyyy: ")
    for cheese in cheeseDictionary:

I know I need to change the dates store din the dictionary into the date time format but I am unsure how to do this. Thanks for any advice given :)

You can easily convert a date to a datetime object. I would suggest a function which you pass the date, shelf life, and date entered by the user. There are timedelta examples in the docs. Also, datetime objects can be subtracted from each other and can be compared (datetime_1 <= datetime_2). For additional help, post back with the function that does this as reading the file, creating the dictionary, etc. does not affect this part of the code.

now =, 8, 6)
difference1 = datetime.timedelta(days=1)
difference2 = datetime.timedelta(weeks=-2)

print "One day in the future is:", now + difference1
#=> One day in the future is: 2003-08-07

print "Two weeks in the past is:", now + difference2
#=> Two weeks in the past is: 2003-07-23
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.