4. os module

MicroPython contains an os module based upon the os module in the Python standard library.
The module provides functions relating to the management of the simple on-device persistent file system and information about the current system.

4.1. listdir

os.listdir()
Returns a list of the names of all the files contained within the local persistent on-device file system.
from microbit import *
import os

# print(os.listdir())
display.scroll(str(os.listdir()), delay=80)

4.2. remove

os.remove(filename)

Removes (deletes) the file named in the argument filename. If the file does not exist an OSError exception will occur.

The code creates a test file, then deletes it by name.
Deleting by name is safer than deleting by list position, which could remove main.py.
from microbit import *
import os

with open('test.txt', 'w') as f:
    f.write('hello')
# print(os.listdir())
display.scroll(str(os.listdir()), delay=80)
os.remove('test.txt')
# print(os.listdir())
display.scroll(str(os.listdir()), delay=80)

4.3. size

os.size(filename)

Returns the size, in bytes, of the file named in the argument filename. If the file does not exist an OSError exception will occur.

The code prints the files with their size.
from microbit import *
import os

files = os.listdir()
for f in files:
    # print(f, os.size(f))
    display.scroll(str(f) + " " + str(os.size(f)), delay=80)

4.4. uname

os.uname()
Returns information identifying the current operating system. The return value is an object with five attributes:
  • sysname - operating system name

  • nodename - name of machine on network (implementation-defined)

  • release - operating system release

  • version - operating system version

  • machine - hardware identifier

from microbit import *
import os

info = os.uname()
# print(info)
display.scroll(str(info), delay=80)
# print("sysname:", info.sysname)
display.scroll("sysname: " + str(info.sysname), delay=80)
# print("nodename:", info.nodename)
display.scroll("nodename: " + str(info.nodename), delay=80)
# print("release:", info.release)
display.scroll("release: " + str(info.release), delay=80)
# print("version:", info.version)
display.scroll("version: " + str(info.version), delay=80)
# print("machine:", info.machine)
display.scroll("machine: " + str(info.machine), delay=80)
The output from the code above was:
(sysname='microbit', nodename='microbit', release='2.1.2', version='micro:bit v2.1.2+0697c6d on 2023-10-30; MicroPython v1.18 on 2023-10-30', machine='micro:bit with nRF52833')
sysname: microbit
nodename: microbit
release: 2.1.2
version: micro:bit v2.1.2+0697c6d on 2023-10-30; MicroPython v1.18 on 2023-10-30
machine: micro:bit with nRF52833

4.5. Micropython version

The micropython that is flashed to the microbit by different software can be determined.
micropython online editor v3 and Thonny v4 flash their version of micropython to the microbit.
os.uname().version returns a string with micropython version in it.
The code below uses a custom function to get the version.
e.g. v1.18 from micro:bit v2.1.0+e4321a8 on 2022-09-26; MicroPython v1.18 on 2022-09-26
e.g. 07b1ed7 from micro:bit v2.1.1+07b1ed7-dirty on 2024-11-25; MicroPython 07b1ed7-dirty on 2024-11-25
The code uses consecutive splits and list indices.
Firstly the string is split at “MicroPython “ and the first index is chosen.
So this could return the string: “v1.18 on 2022-09-26”.
The second split is at the spaces with index 0 being chosen.
The third split is only needed if there is a long version with a “-” in it.
from microbit import *
import os

def micropython_version():
    # micro:bit v2.1.1+07b1ed7-dirty on 2024-11-25; MicroPython 07b1ed7-dirty on 2024-11-25
    info = os.uname().version
    # last split "-" in case version has - in it.
    mpv = info.split("MicroPython ")[1].split()[0].split("-")[0]
    return mpv

mpv = micropython_version()
while True:
    display.scroll(mpv)