[Diaoye learns programming] MicroPython manual built-in module statvfs

Insert image description here

MicroPython is a lightweight version of the interpreter designed to run the Python 3 programming language in embedded systems. Compared with regular Python, the MicroPython interpreter is small (only about 100KB) and is compiled into a binary Executable file to run, resulting in higher execution efficiency. It uses a lightweight garbage collection mechanism and removes most of the Python standard library to accommodate resource-constrained microcontrollers.

The main features of MicroPython include:
1. The syntax and functions are compatible with standard Python, making it easy to learn and use. Supports most of Python's core syntax.
2. Directly access and control the hardware, control GPIO, I2C, SPI, etc. like Arduino.
3. A powerful module system that provides functions such as file system, network, and graphical interface.
4. Support cross-compilation to generate efficient native code, which is 10-100 times faster than the interpreter.
5. The amount of code is small, and the memory usage is small, which is suitable for running on MCU and development boards with small memory.
6. Open source license, free to use. The Shell interactive environment provides convenience for development and testing.
7. The built-in I/O driver supports a large number of microcontroller platforms, such as ESP8266, ESP32, STM32, micro:bit, control board and PyBoard, etc. There is an active community.

The application scenarios of MicroPython include:
1. Quickly build prototypes and user interactions for embedded products.
2. Make some small programmable hardware projects.
3. As an educational tool, it helps beginners learn Python and IoT programming.
4. Build smart device firmware to achieve advanced control and cloud connectivity.
5. Various microcontroller applications such as Internet of Things, embedded intelligence, robots, etc.

Pay attention to the following when using MicroPython:
1. The memory and Flash space are limited.
2. The explanation and execution efficiency is not as good as C language.
3. Some library functions are different from the standard version.
4. Optimize the syntax for the platform and correct the differences with standard Python.
5. Use memory resources rationally and avoid frequently allocating large memory blocks.
6. Use native code to improve the performance of speed-critical parts.
7. Use abstraction appropriately to encapsulate underlying hardware operations.

Generally speaking, MicroPython brings Python into the field of microcontrollers, which is an important innovation that not only lowers the programming threshold but also provides good hardware control capabilities. It is very suitable for the development of various types of Internet of Things and intelligent hardware.

Insert image description here
MicroPython's built-in module statvfs provides a function for obtaining file system status information. Its main features, application scenarios, and matters needing attention are as follows:

Main features: statvfs.statvfs(path) accepts a string parameter path, which represents the path of the file system to obtain status information. If path does not exist, or path is not a valid file system, statvfs.statvfs(path) will throw an exception. statvfs.statvfs(path) returns a tuple containing file system information, such as block size, number of free blocks, maximum file name length, etc.

Application scenario: statvfs.statvfs(path) can be used to obtain and process file system status information in the file system. For example, you can use statvfs.statvfs(path) to get the total size and available space of the file system, or to get the type and flag of the file system.

Things to note: Before using statvfs.statvfs(path), you need to ensure that path is a legal path and path is an existing file system. Otherwise, statvfs.statvfs(path) will fail and throw an exception. Additionally, statvfs.statvfs(path) may have different implementations and support on different platforms. Therefore, when using statvfs.statvfs(path), it is best to check the platform's features and limitations first.

The following are several practical application examples of MicroPython's built-in module statvfs.statvfs(path):

Case 1: Obtain the file system status information of the root directory (/) on the MicroPython board and print it out. code show as below:

import statvfs

# 定义要获取状态信息的文件系统路径
path = '/'

# 获取文件系统状态信息
st = statvfs.statvfs(path)

# 打印文件系统状态信息
print('The status of', path, 'is:')
print('Block size:', st[0], 'bytes')
print('Fragment size:', st[1], 'bytes')
print('Total blocks:', st[2])
print('Free blocks:', st[3])
print('Available blocks:', st[4])
print('Total inodes:', st[5])
print('Free inodes:', st[6])
print('Available inodes:', st[7])
print('Flags:', st[8])
print('Maximum file name length:', st[9])

Case 2: Obtain the total file system size and available space of the SD card (/sd) on the MicroPython board, and calculate the usage rate. code show as below:

import statvfs

# 定义要获取状态信息的文件系统路径
path = '/sd'

# 获取文件系统状态信息
st = statvfs.statvfs(path)

# 获取块大小(字节)
block_size = st[0]

# 获取总块数
total_blocks = st[2]

# 获取可用块数
available_blocks = st[4]

# 计算总大小(字节)
total_size = block_size * total_blocks

# 计算可用空间(字节)
available_space = block_size * available_blocks

# 计算使用率(百分比)
usage_rate = (total_size - available_space) / total_size * 100

# 打印文件系统总大小和可用空间
print('The total size of', path, 'is', total_size, 'bytes')
print('The available space of', path, 'is', available_space, 'bytes')

# 打印文件系统使用率
print('The usage rate of', path, 'is', usage_rate, '%')

Case 3: Determine whether the file system of the root directory (/) supports long file names (length greater than 8) on the MicroPython board. code show as below:

import statvfs

# 定义要获取状态信息的文件系统路径
path = '/'

# 获取文件系统状态信息
st = statvfs.statvfs(path)

# 获取最大文件名长度
max_name_length = st[9]

# 判断是否支持长文件名
if max_name_length > 8:
    print(path, 'supports long file names')
else:
    print(path, 'does not support long file names')

Case 4: Obtain file system information:

import uos
import statvfs

# 获取文件系统信息
fs_stat = uos.statvfs("/")

# 打印文件系统信息
print("总空间:", fs_stat[statvfs.F_BLOCKS] * fs_stat[statvfs.F_BSIZE], "字节")
print("可用空间:", fs_stat[statvfs.F_BAVAIL] * fs_stat[statvfs.F_BSIZE], "字节")
print("使用空间:", (fs_stat[statvfs.F_BLOCKS] - fs_stat[statvfs.F_BFREE]) * fs_stat[statvfs.F_BSIZE], "字节")

In this example, we use the statvfs function of the uos module to obtain root file system information. By accessing the properties of the statvfs object, we can obtain the total space, free space, and used space of the file system. We use fs_stat[statvfs.F_BLOCKS] * fs_stat[statvfs.F_BSIZE] to calculate the total space, use fs_stat[statvfs.F_BAVAIL] * fs_stat[statvfs.F_BSIZE] to calculate the available space, and use (fs_stat[statvfs.F_BLOCKS] - fs_stat[ statvfs.F_BFREE]) * fs_stat[statvfs.F_BSIZE] to calculate the used space and print out the file system information.

Case 5: Check the maximum file name length supported by the file system:

import uos
import statvfs

# 获取文件系统信息
fs_stat = uos.statvfs("/")

# 获取文件系统支持的最大文件名长度
max_filename_length = fs_stat[statvfs.F_NAMEMAX]

# 打印最大文件名长度
print("文件系统支持的最大文件名长度:", max_filename_length)

In this example, we use the statvfs function of the uos module to obtain root file system information. By accessing the properties of the statvfs object, we can obtain the maximum file name length supported by the file system. We use fs_stat[statvfs.F_NAMEMAX] to get the maximum filename length and print out the value.

Case 6: Get the block size of the file system:

import uos
import statvfs

# 获取文件系统信息
fs_stat = uos.statvfs("/")

# 获取文件系统的块大小
block_size = fs_stat[statvfs.F_BSIZE]

# 打印块大小
print("文件系统的块大小:", block_size, "字节")

In this example, we use the statvfs function of the uos module to obtain root file system information. By accessing the properties of the statvfs object, we can obtain the block size of the file system. We use fs_stat[statvfs.F_BSIZE] to get the block size and print out the value.

Case 7: Obtain file system capacity information

import uos

# 获取文件系统的容量信息
fs_stat = uos.statvfs("/")
total_space = fs_stat[0] * fs_stat[2]  # 总空间 = 每个块的大小 * 总块数
free_space = fs_stat[0] * fs_stat[3]   # 可用空间 = 每个块的大小 * 可用块数

print("总空间:", total_space, "字节")
print("可用空间:", free_space, "字节")

This example uses the uos.statvfs() function to obtain the capacity information of the root directory file system and calculates the total space and free space from the returned tuple.

Case 8: Get the file system block size

import uos

# 获取文件系统块的大小
fs_stat = uos.statvfs("/")
block_size = fs_stat[0]

print("块大小:", block_size, "字节")

In this example, we use the uos.statvfs() function to get the capacity information of the root file system and extract the block size from the returned tuple.

Case 9: Calculate file system usage

import uos

# 获取文件系统容量信息
fs_stat = uos.statvfs("/")
total_blocks = fs_stat[2]
free_blocks = fs_stat[3]

# 计算文件系统使用率
usage_percent = (total_blocks - free_blocks) / total_blocks * 100

print("文件系统使用率:", usage_percent, "%")

In this example, we use the uos.statvfs() function to obtain the capacity information of the root directory file system and calculate the file system usage. Usage is calculated as the ratio of used blocks to total blocks and multiplied by 100 to express it as a percentage.

These cases demonstrate the practical application of the uos.statvfs() function, including obtaining file system capacity information, obtaining file system block size, and calculating file system usage. By using the statvfs function, you can easily obtain file system-related information on the MicroPython device and perform corresponding processing and calculations. Please note that when using the statvfs function, please ensure that you have sufficient permissions and reasonable path settings, and make appropriate adjustments and processing according to actual needs.

Insert image description here

おすすめ

転載: blog.csdn.net/weixin_41659040/article/details/132787286