This article is an implementation of the MicroPython file system using the esp8266 and esp32 microcontroller boards as an experimental board. The file system usage involves directories and files including connecting the device to be seen as a MicroPython file system, for example, connecting to an SD-Card to see it as a system directory, etc. It uses the os class to create, open, access, write data and disable files which will be part of MicroPython’s file class.
os class
The list of methods belonging to the os class is shown in Figure 1. There are several groups of instructions. Instead, this article discusses the commands related to directories and files: listdir, mkdir, rmdir, chdir, getcwd, remove, and rename.
listdir()
The command to list directories and files is listdir(), which can be called without arguments or parameters or specify the directory. For example, the code below commands to list directories and files in the current address and command to list the addresses in the root directory or / and the result is as shown in Figure 2.
import os
print(os.listdir())
print(os.listdir('/'))
mkdir()
The mkdir command is used to create directories. Specifies the name of the directory or path as an argument to the command. For example, the following example creates a directory ex at the root (/) and then creates directories a and b in another layer of ex. The result is shown in Figure 3.
os.mkdir('/ex')
os.mkdir('/ex/a')
os.mkdir('/ex/b')
print(os.listdir())
print(os.listdir('/ex'))
chdir()
The chdir() command is used to move the location of the current working directory to the desired location by specifying the name of the desired directory as the command argument. And there are some specially designated marks for references to directories:
- . represents current directory
- / represents root directory
- .. represents parent directory
An example program to use is as follows. And the result of the work is as shown in Figure 4.
os.chdir('/ex')
print(os.listdir())
os.chdir('./a')
print(os.listdir())
os.chdir('..')
print(os.listdir())
os.chdir('/')
print(os.listdir())
getcwd()
The getcwd() command is used to read the current directory location as in the example, the current directory location is read and stored in variable a. After that, try to move to /ex and report the name again as shown in Figure 5.
a = os.getcwd()
print(a)
os.chdir('/ex')
print(os.getcwd())
rmdir()
The rmdir() command is used to delete the directory specified in the parameter. For example, the following example deletes directory b which is in another layer of directory ex. After listing files and directories in ex, only directory a will be found, as shown in Figure 6.
os.rmdir('/ex/b')
print(os.listdir('/ex'))
rename()
The rename() command is used to change the name of a directory or file. You must define two arguments:
- source
- destination
The command renames ‘source’ to ‘destination’. For example, renames a directory from /ex/a to /ex/seta, so that when displays the list, it showed seta instead of a, as shown in Figure 7.
os.rename('/ex/a','/ex/seta')
print(os.listdir('/ex'))
remove()
The command remove() is used to delete the specified files or directories, as below, delete the /ex/seta directory, and then delete the /main.py file.
os.remove('/ex/seta')
>>> print(os.listdir('/ex'))
[]
>>> print(os.listdir('/'))
['boot.py', 'ex', 'main.py', 'testIO.py']
>>> os.remove('/main.py')
>>> print(os.listdir('/'))
File class
MicroPython in a microcontroller esp32/esp8266 supports FAT and littlefs v2 file services which allows programmers to use the text file commands used in this article:
- open()
- close()
- write()
- read()
Activating a file requires creating an object for reference to that file according to the following build pattern.
obj = open(file, mode)
The opening format is as follows.
- w for creating
- r for reading
- a for appending
To disable an open file, use the close() command as follows:
obj.close()
Reading uses the read() command to read data from a file. All of them have the following formats:
data = obj.read()
And writing data to a file can be done with the write() command:
obj.write( data )
Example Code
The following program creates a log file for collecting temperature and humidity values from the DHT22 sensor by storing the results in a file named dht22log.txt and appending it after the old data.
#############################################
# SaveDHT2File.py
# (C) 2021, JarutEx
# 2021-09-15
#############################################
import os
import dht
from machine import Pin
dht22 = dht.DHT22(Pin(15))
filename = 'dht22log.txt'
if (filename in os.listdir()):
f = open(filename,'a')
else:
f = open(filename,'w')
dht22.measure()
print("Write...")
f.write("Temperature ....: {}C, Humidity .......: {}%\n".format(
dht22.temperature(),
dht22.humidity())
)
f.close()
print("Read...")
f = open(filename,'r')
print(f.read())
f.close()
The result is show in Figure 9.
Conclusion
From this article, you will find that MicroPython’s Python language makes programming more convenient because MicroPython acts like a small operating system, allowing us to run commands and can be used in a variety of applications, such as temporarily storing data to a file when data cannot be sent to the network, or as a log of operations to analyze the operation of a program written later, etc. Finally, have fun. programming.
If you want to talk with us, feel free to leave comments below!!
Reference
- MicroPython : The Internal filesystem
(C) 2021, By Jarut Busarathid and Danai Jedsadathitikul
Updated 2021-12-09