import glob from random import randrange import sqlite3 BING_IMGS_PATH = "bing/*" DB = "bing.db" def get_image_count(): # Choose a random image bing_files = glob.glob(BING_IMGS_PATH) image_idx = randrange(len(bing_files)) image = bing_files[image_idx] # Get the latest count and increment con = sqlite3.connect(DB) cur = con.cursor() cur.execute("UPDATE BING_COUNT SET COUNT = COUNT + 1") con.commit() result = cur.execute("SELECT COUNT FROM BING_COUNT") count = result.fetchone() count = count[0] cur.close() con.close() return(image, count)