0

$USD

$USD
TUTORIALS micro:bitMicroPython

micro:bit MicroPython Tutorial: creating a file in the filesystem

DFRobot Mar 11 2019 1131

In this microbit tutorial we will learn how to create a file on the micro:bit file system. We will also write some content to it.

Introduction

In this microbit tutorial we will learn how to create a file on the micro:bit file system. We will also write some content to it.

Data written on the micro:bit file system is persistent [1], meaning that it will be kept even if we power off the device. Note however that since the file system is stored on the flash memory, re-flashing the device will erase all the data from the file system [1].

It’s also important to take in consideration that MicroPython on the micro:bit offers a flat file system, meaning that it doesn’t have the concept of directories. So, the file system is simply a list of named files [1].

The code

We will start by importing the os module, which will allow us to list all the files on the file system. This way, we will be able to later confirm that the file was indeed written to the file system.

import os

Then we are going to call the listdir function of the imported module, which lists the name of all the files on the device file system [2]. We will  print the returned list directly.

print(os.listdir())

After this, we will call the open function, passing as first input the name of the file we want to create and as second input the opening mode. We will call our file “myfile.txt”.

Since we want to open the file for writing content to it, we should pass “w” as opening mode. Note that, when opening the file in writing mode, if the file already exists then its content is overwritten [3]. In our case, since the file doesn’t exit yet, it will be created.

As output, this function call will return an object [3] that we will be able to use to write content to the file.

Note that, by default, the file will be opened in text mode. Nonetheless, it can also be opened in binary mode, as can be seen in the documentation of the open function.

file = open("myfile.txt", "w")

So, in order to write content to the file, we simply need to call the write method on the returned object, passing as input a string with the content we want to write.

file.write("hello world")

After writing the content to the file, we will simply close it with a call to the close method.

file.close()

After writing the content to the file, we will simply close it with a call to the close method.

file.close()

To finalize, we will print again the list of files in the file system. This second print should now show an additional file called “myfile.txt”.

print(os.listdir())

The final source code can be seen below.

import os
 
print(os.listdir())
 
file = open("myfile.txt", "w")
 
file.write("hello world")
 
file.close()
 
print(os.listdir())

Testing the code

To test the code, simply run the previous script on your micro:bit board, using a tool of your choice. I’ll be using uPyCraft, a MicroPython IDE.

After running the code, you should see a result similar to figure 1, which shows both the list of files before and after the creation of the “myfile.txt” file. As can be seen, the file was successfully created, as expected.


https://techtutorialsx.com/2019/02/10/microbit-micropython-creating-a-file-in-the-filesystem/

References

[1] https://microbit-micropython.readthedocs.io/en/latest/filesystem.html

[2] https://microbit-micropython.readthedocs.io/en/latest/os.html#os.listdir

[3] https://microbit-micropython.readthedocs.io/en/latest/filesystem.html#open