Load a text file from the SD card for the Adafruit PyPortal.

This is some basic code that will print a list of files on the SD card and load a given text files contents. Then it converts the JSON text into a dictionary that makes it easy to list out and display values.

import time
import json

import os
import board
from adafruit_pyportal import PyPortal

# Create the PyPortal object
pyportal = PyPortal(status_neopixel=board.NEOPIXEL)

# Default location to look is in internal memory
FILE_DIRECTORY = "/sd"

RECIPE_FILE = "Cookies"

def print_directory(path, tabs=0):
    for file in os.listdir(path):
        stats = os.stat(path + "/" + file)
        filesize = stats[6]
        isdir = stats[0] & 0x4000

        if filesize < 1000:
            sizestr = str(filesize) + " by"
        elif filesize < 1000000:
            sizestr = "%0.1f KB" % (filesize / 1000)
        else:
            sizestr = "%0.1f MB" % (filesize / 1000000)

        prettyprintname = ""
        for _ in range(tabs):
            prettyprintname += "   "
        prettyprintname += file
        if isdir:
            prettyprintname += "/"
        print('{0:<20} Size: {1:>6}'.format(prettyprintname, sizestr))

        # recursively print directory contents
        if isdir:
            print_directory(path + "/" + file, tabs + 1)

try:
    print_directory(FILE_DIRECTORY)
except OSError as error:
    raise Exception("No files found on flash or SD Card")
    
try:
    with open("/sd/" + RECIPE_FILE + ".txt", "r") as fp:
        x = fp.read()
        # parse x:
        y = json.loads(x)
        ingredients = y["ingredients"]
        for i in ingredients:
            print('{} - {}'.format(i.get("quantity"), i.get("ingredient")))
        
except OSError as e:
    raise Exception("Could not read text file.")

On the MicroSD card there is a file named Cookies.txt that is made up of the following JSON.

{
   "ingredients":[
      {
         "quantity":"1 cup",
         "ingredient":"butter, softened"
      },
      {
         "quantity":"1 cup",
         "ingredient":"white sugar"
      },
      {
         "quantity":"1 cup",
         "ingredient":"packed brown sugar"
      },
      {
         "quantity":"Two",
         "ingredient":"eggs"
      },
      {
         "quantity":"2 tsp",
         "ingredient":"vanilla extract"
      },
      {
         "quantity":"1 tsp",
         "ingredient":"baking soda"
      },
      {
         "quantity":"2 tsp",
         "ingredient":"hot water"
      },
      {
         "quantity":"1/2 tsp",
         "ingredient":"salt"
      },
      {
         "quantity":"3 cups",
         "ingredient":"all-purpose flour"
      },
      {
         "quantity":"2 cups",
         "ingredient":"semisweet chocolate chips"
      },
      {
         "quantity":"1 cup",
         "ingredient":"chopped walnuts"
      }
   ]
}

This should result in the following output:

code.py output:
ESP firmware: bytearray(b'1.2.2\x00')
Set background to  0
Coleslaw Dressing.txt Size: 426 by
Bread Dough.txt      Size: 426 by
Pancakes.txt         Size: 426 by
Cookies.txt          Size: 1.0 KB
1 cup - butter, softened
1 cup - white sugar
1 cup - packed brown sugar
Two - eggs
2 tsp - vanilla extract
1 tsp - baking soda
2 tsp - hot water
1/2 tsp - salt
3 cups - all-purpose flour
2 cups - semisweet chocolate chips
1 cup - chopped walnuts