littlefs-python

0.17.1 · active · verified Sun Apr 12

littlefs-python is a Python wrapper for the littlefs filesystem, a high-integrity embedded filesystem designed for microcontrollers. It provides both a high-level Pythonic interface and a low-level C-style API, enabling the creation, inspection, and modification of littlefs binary images. Currently at version 0.17.1, the library is actively maintained with a regular release cadence.

Warnings

Install

Imports

Quickstart

This example demonstrates creating a new littlefs image, writing files and directories using the high-level Pythonic API, listing contents, and finally dumping the filesystem to a binary file.

from littlefs import LittleFS

# Initialize the File System according to your specifications
# block_size and block_count must match the embedded system's flash parameters.
fs = LittleFS(block_size=512, block_count=256)

# Open a file and write some content
with fs.open('first-file.txt', 'w') as fh:
    fh.write('Hello, LittleFS from Python!\n')

# Create a directory
fs.mkdir('configs')

# Write another file in the new directory
with fs.open('configs/settings.txt', 'w') as fh:
    fh.write('setting_key=value\n')

# List contents of the root directory
print("Root directory contents:", fs.listdir('/'))

# Read content back
with fs.open('first-file.txt', 'r') as fh:
    content = fh.read()
    print("Content of first-file.txt:", content)

# Dump the filesystem content to a file (e.g., for flashing to a device)
with open('FlashMemory.bin', 'wb') as fh:
    fh.write(fs.context.buffer)

print("Filesystem image 'FlashMemory.bin' created.")

view raw JSON →