{"id":2571,"date":"2020-07-27T21:45:27","date_gmt":"2020-07-27T21:45:27","guid":{"rendered":"https:\/\/www.richa1.com\/RichardAlbritton\/?p=2571"},"modified":"2020-07-27T21:45:27","modified_gmt":"2020-07-27T21:45:27","slug":"load-a-text-file-from-the-sd-card-for-the-adafruit-pyportal","status":"publish","type":"post","link":"https:\/\/www.richa1.com\/RichardAlbritton\/load-a-text-file-from-the-sd-card-for-the-adafruit-pyportal\/","title":{"rendered":"Load a text file from the SD card for the Adafruit PyPortal."},"content":{"rendered":"\n<p>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.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import time\nimport json\n\nimport os\nimport board\nfrom adafruit_pyportal import PyPortal\n\n# Create the PyPortal object\npyportal = PyPortal(status_neopixel=board.NEOPIXEL)\n\n# Default location to look is in internal memory\nFILE_DIRECTORY = \"\/sd\"\n\nRECIPE_FILE = \"Cookies\"\n\ndef print_directory(path, tabs=0):\n    for file in os.listdir(path):\n        stats = os.stat(path + \"\/\" + file)\n        filesize = stats&#91;6]\n        isdir = stats&#91;0] &amp; 0x4000\n\n        if filesize &lt; 1000:\n            sizestr = str(filesize) + \" by\"\n        elif filesize &lt; 1000000:\n            sizestr = \"%0.1f KB\" % (filesize \/ 1000)\n        else:\n            sizestr = \"%0.1f MB\" % (filesize \/ 1000000)\n\n        prettyprintname = \"\"\n        for _ in range(tabs):\n            prettyprintname += \"   \"\n        prettyprintname += file\n        if isdir:\n            prettyprintname += \"\/\"\n        print('{0:&lt;20} Size: {1:>6}'.format(prettyprintname, sizestr))\n\n        # recursively print directory contents\n        if isdir:\n            print_directory(path + \"\/\" + file, tabs + 1)\n\ntry:\n    print_directory(FILE_DIRECTORY)\nexcept OSError as error:\n    raise Exception(\"No files found on flash or SD Card\")\n    \ntry:\n    with open(\"\/sd\/\" + RECIPE_FILE + \".txt\", \"r\") as fp:\n        x = fp.read()\n        # parse x:\n        y = json.loads(x)\n        ingredients = y&#91;\"ingredients\"]\n        for i in ingredients:\n            print('{} - {}'.format(i.get(\"quantity\"), i.get(\"ingredient\")))\n        \nexcept OSError as e:\n    raise Exception(\"Could not read text file.\")<\/code><\/pre>\n\n\n\n<p>On the MicroSD card there is a file named Cookies.txt that is made up of the following JSON.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n   \"ingredients\":&#91;\n      {\n         \"quantity\":\"1 cup\",\n         \"ingredient\":\"butter, softened\"\n      },\n      {\n         \"quantity\":\"1 cup\",\n         \"ingredient\":\"white sugar\"\n      },\n      {\n         \"quantity\":\"1 cup\",\n         \"ingredient\":\"packed brown sugar\"\n      },\n      {\n         \"quantity\":\"Two\",\n         \"ingredient\":\"eggs\"\n      },\n      {\n         \"quantity\":\"2 tsp\",\n         \"ingredient\":\"vanilla extract\"\n      },\n      {\n         \"quantity\":\"1 tsp\",\n         \"ingredient\":\"baking soda\"\n      },\n      {\n         \"quantity\":\"2 tsp\",\n         \"ingredient\":\"hot water\"\n      },\n      {\n         \"quantity\":\"1\/2 tsp\",\n         \"ingredient\":\"salt\"\n      },\n      {\n         \"quantity\":\"3 cups\",\n         \"ingredient\":\"all-purpose flour\"\n      },\n      {\n         \"quantity\":\"2 cups\",\n         \"ingredient\":\"semisweet chocolate chips\"\n      },\n      {\n         \"quantity\":\"1 cup\",\n         \"ingredient\":\"chopped walnuts\"\n      }\n   ]\n}<\/code><\/pre>\n\n\n\n<p>This should result in the following output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>code.py output:\nESP firmware: bytearray(b'1.2.2\\x00')\nSet background to  0\nColeslaw Dressing.txt Size: 426 by\nBread Dough.txt      Size: 426 by\nPancakes.txt         Size: 426 by\nCookies.txt          Size: 1.0 KB\n1 cup - butter, softened\n1 cup - white sugar\n1 cup - packed brown sugar\nTwo - eggs\n2 tsp - vanilla extract\n1 tsp - baking soda\n2 tsp - hot water\n1\/2 tsp - salt\n3 cups - all-purpose flour\n2 cups - semisweet chocolate chips\n1 cup - chopped walnuts<\/code><\/pre>\n<div class=\"sharedaddy sd-sharing-enabled\"><div class=\"robots-nocontent sd-block sd-social sd-social-icon-text sd-sharing\"><h3 class=\"sd-title\">Share this:<\/h3><div class=\"sd-content\"><ul><li class=\"share-print\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"\" class=\"share-print sd-button share-icon\" href=\"https:\/\/www.richa1.com\/RichardAlbritton\/load-a-text-file-from-the-sd-card-for-the-adafruit-pyportal\/\" target=\"_blank\" title=\"Click to print\"><span>Print<\/span><\/a><\/li><li class=\"share-email\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"\" class=\"share-email sd-button share-icon\" href=\"https:\/\/www.richa1.com\/RichardAlbritton\/load-a-text-file-from-the-sd-card-for-the-adafruit-pyportal\/?share=email\" target=\"_blank\" title=\"Click to email this to a friend\"><span>Email<\/span><\/a><\/li><li class=\"share-facebook\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-facebook-2571\" class=\"share-facebook sd-button share-icon\" href=\"https:\/\/www.richa1.com\/RichardAlbritton\/load-a-text-file-from-the-sd-card-for-the-adafruit-pyportal\/?share=facebook\" target=\"_blank\" title=\"Click to share on Facebook\"><span>Facebook<\/span><\/a><\/li><li class=\"share-twitter\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-twitter-2571\" class=\"share-twitter sd-button share-icon\" href=\"https:\/\/www.richa1.com\/RichardAlbritton\/load-a-text-file-from-the-sd-card-for-the-adafruit-pyportal\/?share=twitter\" target=\"_blank\" title=\"Click to share on Twitter\"><span>Twitter<\/span><\/a><\/li><li class=\"share-end\"><\/li><\/ul><\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>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. On the MicroSD card there is a file named Cookies.txt that is made &hellip; <a href=\"https:\/\/www.richa1.com\/RichardAlbritton\/load-a-text-file-from-the-sd-card-for-the-adafruit-pyportal\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Load a text file from the SD card for the Adafruit PyPortal.<\/span><\/a><\/p>\n<div class=\"sharedaddy sd-sharing-enabled\"><div class=\"robots-nocontent sd-block sd-social sd-social-icon-text sd-sharing\"><h3 class=\"sd-title\">Share this:<\/h3><div class=\"sd-content\"><ul><li class=\"share-print\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"\" class=\"share-print sd-button share-icon\" href=\"https:\/\/www.richa1.com\/RichardAlbritton\/load-a-text-file-from-the-sd-card-for-the-adafruit-pyportal\/\" target=\"_blank\" title=\"Click to print\"><span>Print<\/span><\/a><\/li><li class=\"share-email\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"\" class=\"share-email sd-button share-icon\" href=\"https:\/\/www.richa1.com\/RichardAlbritton\/load-a-text-file-from-the-sd-card-for-the-adafruit-pyportal\/?share=email\" target=\"_blank\" title=\"Click to email this to a friend\"><span>Email<\/span><\/a><\/li><li class=\"share-facebook\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-facebook-2571\" class=\"share-facebook sd-button share-icon\" href=\"https:\/\/www.richa1.com\/RichardAlbritton\/load-a-text-file-from-the-sd-card-for-the-adafruit-pyportal\/?share=facebook\" target=\"_blank\" title=\"Click to share on Facebook\"><span>Facebook<\/span><\/a><\/li><li class=\"share-twitter\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-twitter-2571\" class=\"share-twitter sd-button share-icon\" href=\"https:\/\/www.richa1.com\/RichardAlbritton\/load-a-text-file-from-the-sd-card-for-the-adafruit-pyportal\/?share=twitter\" target=\"_blank\" title=\"Click to share on Twitter\"><span>Twitter<\/span><\/a><\/li><li class=\"share-end\"><\/li><\/ul><\/div><\/div><\/div>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[161,191,57,64],"tags":[190,189],"jetpack_featured_media_url":"","jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p5AhH6-Ft","_links":{"self":[{"href":"https:\/\/www.richa1.com\/RichardAlbritton\/wp-json\/wp\/v2\/posts\/2571"}],"collection":[{"href":"https:\/\/www.richa1.com\/RichardAlbritton\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.richa1.com\/RichardAlbritton\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.richa1.com\/RichardAlbritton\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.richa1.com\/RichardAlbritton\/wp-json\/wp\/v2\/comments?post=2571"}],"version-history":[{"count":1,"href":"https:\/\/www.richa1.com\/RichardAlbritton\/wp-json\/wp\/v2\/posts\/2571\/revisions"}],"predecessor-version":[{"id":2572,"href":"https:\/\/www.richa1.com\/RichardAlbritton\/wp-json\/wp\/v2\/posts\/2571\/revisions\/2572"}],"wp:attachment":[{"href":"https:\/\/www.richa1.com\/RichardAlbritton\/wp-json\/wp\/v2\/media?parent=2571"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.richa1.com\/RichardAlbritton\/wp-json\/wp\/v2\/categories?post=2571"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.richa1.com\/RichardAlbritton\/wp-json\/wp\/v2\/tags?post=2571"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}