Getting Started with Pyboard's Filesystem

Contents

The /flash Filesystem

This article discusses the internal filesystem of the Pyboard V1.1 and assumes there isn't a microSD card inserted in the card slot on the board. This article describes how to use a microSD card on the Pyboard.

MicroPython on first install automatically creates a small internal filesystem on the Pyboard's flash memory. This filesystem is preserved on subsequent MicroPython firmware upgrades.

Each the time the Pyboard is powered up this filesystem is mounted as /flash. It is tiny with only a total storage of 95 KB available.

If the Pyboard is connected to a computer this filesystem appears as a drive that's usually named PYBFLASH.

The filesystem can be seen and operated upon from the Thonny Files pane.

Pyboard Boot Sequence

When the MicroPython system becomes active after power-up or a reset it begins the boot sequence. if there is no microSD card present the local /flash filesystem is mounted and becomes the current directory.

  1. If the file boot.py is found then it is run.

    The main purpose of boot.py is to handle low-level, one-off configuration and initialization of the Pyboard's hardware before the main application code runs. This sets up the fundamental operating environment for the rest of your program.
  2. If the file main.py is found then it is run.

    This could be the application that the Pyboard runs - especially if it is now considered to be operating as a production system.
  3. The REPL becomes available for running direct MicroPython statements if the Pyboard is connected to a computer.

    MicroPython scripts can be written using an IDE such as Thonny and sent to the Pyboard for execution.

Neither of these two files need be present, in which case the REPL becomes immediately available after the filesystem is mounted.

MicroPython os Module

The MicroPython os module provides a basic set of commands to perform filesystem operations. Directory listings, basic statistics, creating new directories, deleting / renaming directories and files are all possible.

This article, MicroPython 'os' Module, provides an introduction to this module.

Reading & Writing Files with MicroPython

MicroPython provides the built-in open() function to interact with files stored on the microcontroller's file system. Files can be created from scratch for writing. Existing files can be opened for reading or appending extra content. Content can be text or binary.

The open() function behaves like a class constructor by returning an object. This file object has useful methods for reading and writing to the physical file.

Many MicroPython firmware ports also support random access for reading. This allows the file pointer to be moved to any part of the file from which any number of subsequent bytes can be read.

This article, MicroPython: Reading & Writing Files, provides an introduction to this topic.