Set CircuitPython file system Writable, and write/read text file on CircuitPython.

This code simple try to write a text file in CircuitPython file system and read back, tested on Raspberry Pi Pico 2 W running CircuitPython 9.2.1.

cpy_write_and_read_text_file.py
"""
CircuitPython exercise to write and read text file.

Tested on Raspberry Pi Pico 2 W/CircuitPython 9.2.1
"""
print("- To write hello.txt -")
try:
    with open("/hello.txt", "w") as fp:
        fp.write("hello, world! from coXXect")
except OSError as err:
    print(err)

print("- To read hello.txt 0")
try:
    with open("/hello.txt", "r") as fp:
        print(fp.readline())
except OSError as err:
    print(err)

It will raise OSError of Read-only filesystem.

Because by default in CircuitPython, filesystem is read-only for running code.

cpy_check_vfs_readonly.py, check if the filesystem is readonly or not.
"""
CircuitPython code to check if Filesystem is Read Only or Writable.

Tested on Raspberry Pi Pico 2 W/CircuitPython 9.2.1
"""

import storage

vfs = storage.getmount("/")

if vfs.readonly:
    print("Filesystem is Read Only")
else:
    print("Filesystem is Writable")


boot_writable.py, to make it writable, save this file in CIRCUITPY "/", named boot.py.
And reset.
"""
By default, the filesystem of CircuitPython is Read Only.
To make it writable, save this file in CIRCUITPY "/", named boot.py.
And reset.

ref:
CircuitPython Essentials > CircuitPython Storage
https://learn.adafruit.com/circuitpython-essentials/circuitpython-storage

https://learn.adafruit.com/cpu-temperature-logging-with-circuit-python/writing-to-the-filesystem


"""

import storage

# Remount the filesystem as writable
storage.remount("/", readonly=False)


cpy_reset.py, reset using CircuitPython code.
"""
microcontroller.reset()
Reset the microcontroller.
After reset, the microcontroller will enter the run mode last set by on_next_reset.

* Warning *
This may result in file system corruption when connected to a host computer.
Be very careful when calling this! Make sure the device “Safely removed” on Windows
or “ejected” on Mac OSX and Linux.

ref:
https://docs.circuitpython.org/en/latest/shared-bindings/microcontroller/index.html#microcontroller.reset
"""

import microcontroller

microcontroller.reset()




Comments

Popular posts from this blog

480x320 TFT/ILI9488 SPI wih EP32C3 (arduino-esp32) using Arduino_GFX Library

my dev.tools - FNIRSI 2C23T 3-in-1 Dual Channel Oscilloscope/Multimeter/Signal Generator