Micro:bit MicroPython: reading a file from the file system

In this tutorial we will check how to read the content of a file from the micro:bit file system, using MicroPython.

Introduction

In this tutorial we will check how to read the content of a file from the micro:bit file system, using MicroPython. For an introduction on how to create and write content to a file, please check this previous tutorial.

The code

We will start our code by writing a file to the file system, so we know that there is a file to read. Nonetheless, if you have already created a file on the file system by following the previous tutorial, you can skip this part and read that file.

As covered on the mentioned tutorial, we will first call the open function, passing as first input the name of the file and as second input the opening mode. We will call our file test.txt and we will open the file in write mode by passing “w” as second argument of the open function.

As output, the open function will return an object of class TextIO, which we will be using to write content to the file. So, we will call the write method on the mentioned object, passing as input a string with the content to write. We will just write a simple test string.

Finally, we will close the file by calling the close method on the TextIO object.

file = open("test.txt", "w")
file.write("test content")
file.close()

Now that we have the file on the file system, we will open it again and read its content. So, we will call the open method again, passing as input the name of the file. We don’t need to explicitly pass the opening mode since it defaults to reading [1].

Nonetheless, if we wanted to specify the mode argument, we would need to pass the value “r” to open the file in reading mode.

We will store the TextIO object returned by the open function in a variable, since we will need it to read the content of the file.

 
fileToRead = open("test.txt")

To read the content of the file, we simply need to call the read method on our object. As output, this method will return the content of the file. We will then print the content.

Note that, optionally, we can pass as input of the method the number of characters we want to read if we don’t want to read the whole file [2]. Nonetheless, in our case, we will read the whole content.

content = fileToRead.read()
print(content)

To finalize, we will close the file by calling the close method on our TextIO object.

fileToRead.close()

The final code can be seen below.

file = open("test.txt", "w")
file.write("test content")
file.close()

fileToRead = open("test.txt")

content = fileToRead.read()
print(content)

fileToRead.close()

Testing the code

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

You should get an output similar to figure 1, which shows the result of running the script. As can be seen, the content of the file we have previously written gets printed to the console, as expected.

Micro:bit MicroPython read file from file system

Figure 1 – Reading a file from the micro:bit file system.

References

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

[2] https://microbit-micropython.readthedocs.io/en/latest/filesystem.html#BytesIO.read

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s