Picking a random recipe for your cooking inspiration

If, like us, you enjoy cooking and you have collected hoarded hundreds (if not thousands) of various delicious recipes over the years, choosing what to eat for the week can be overwhelming.

Of course, you could go ahead and classify everything nicely. But that would be a tremendous task and, here at urbanhacker.net , we want an easy and simple solution :).

Why not picking randomly a few recipes each week for our culinary inspiration?

Inspiring isn’t it!

What do you need to hack this thing?

  • A collection of unorganized recipes in a folder 😛
  • Python installed and running

In my case, my recipes are already classified in folders (dessert, pasta, vegetarian, meat, etc). This will simplify the script and from now on I am going to assume that you have a similar classification.

Then, you will need to get a list of all the files in the recipes folders.

import os

# Getting a list of all files in the recipes folder
recipes_dir = os.getcwd()

# r=root, d=directories, f = files
recipes = []
for r, d, f in os.walk(recipes_dir):
    for file in f:
        recipes.append(os.path.join(r, file))

for recipe in recipes:

Let’s see how this looks:

(Please bare in mind that some of my recipes are in French, thus the non-rendered characters). You now have a list of the recipes, but also some other unwanted files like Thumbs.db are listed. So, you will need to filter those. FYI, my recipes are mainly .pdf and occasionally .jpg.

Another improvement you can do is to remove the full path of each entry. Currently they look like: C:\Users\micro\Documents\recipes\Meat\Chili con carne.pdf. You don’t need the full path. So, you can remove the common part, but I’d advice you to keep the recipe’s folder, so that you know its category.

New code:

import os

# Getting a list of all files in the recipes folder
recipes_dir = os.getcwd()

# r=root, d=directories, f = files
recipes = []
for r, d, f in os.walk(recipes_dir):
    for file in f:
        filename = os.path.join(r, file)
        # Remove common part of the path
        filename = filename.replace("C:\\Users\\micro\\Documents\\recipes\\", "")

        # Only display .pdf or .jpg. All my recipes are either pdf or images
        if ".pdf" in filename or ".jpg" in filename:

for recipe in recipes:
  print(" ", recipe)


Now you can get a few random recipes from the list using random.choice().

for i in range(0, 5):
  recipe = random.choice(recipes)
  print("   ", recipe)

Don’t forget to import the random module at the beginning of the script.

Okay! This works, but there are some drawbacks:

First, it could happen that you get more than one recipe from the same folder (e.g. 5 dessert recipes and no recipe with meat, or only recipes for salads).

Second, a better interface is needed. The console is fine for a quick proof of concept, but if you aren’t into retro computing, you might want something probably slightly fancier…. How to develop a better interface will be part of a second tutorial. Stay tuned for more!


5 comments / Add your comment below

  1. Does your site have a contact page? I’m having trouble locating it but, I’d like to shoot you an e-mail.
    I’ve got some recommendations for your blog you
    might be interested in hearing. Either way, great website
    and I look forward to seeing it expand over time.

  2. Hello, i read your blog occasionally and i own a similar one and
    i was just wondering if you get a lot of spam responses?
    If so how do you protect against it, any plugin or anything you can recommend?
    I get so much lately it’s driving me insane so any assistance is very much appreciated.

    1. Spam is a complicated topic especially because I do not want to have lots of captcha and make it complicated to comment on my blog.

Leave a Reply

Your email address will not be published.