import time import json if __name__ == '__main__': while True: cardID = raw_input("Enter card ID: ") event = {} event["time"] = time.time() event["cardID"] = cardID event["name"] = '' try: with open('cards/' + cardID + '.json') as card_data: data = json.load(card_data) event["name"] = data['name'] except: print "Card not registered." history = [] try: with open('history.json', 'r') as json_data: history = json.load(json_data) except: print 'Initializing new history file.' finally: history.append(event) with open('history.json', 'w') as outfile: json.dump(history, outfile)