How to Check Free Space on an ESP32 or ESP8266 Using MicroPython?

Estimated read time 2 min read

To check the free space on an ESP32 or ESP8266 using MicroPython, you can utilize the uos module (which provides access to the underlying operating system functions). Specifically, you can use the uos.statvfs() function to obtain information about the file system, including free space.

Here’s an example code snippet that demonstrates how to check the free space on an ESP32 or ESP8266 using MicroPython:

import uos

# Get the file system information
fs_stat = uos.statvfs('/')

# Calculate the free space in bytes
free_space = fs_stat[0] * fs_stat[3]

# Convert the free space to a more readable format
free_space_kb = free_space / 1024
free_space_mb = free_space_kb / 1024
free_space_gb = free_space_mb / 1024

# Print the free space information
print("Free Space:")
print("  Bytes:", free_space)
print("  Kilobytes:", free_space_kb)
print("  Megabytes:", free_space_mb)
print("  Gigabytes:", free_space_gb)

This code snippet retrieves the file system information using uos.statvfs('/') to query the root directory. The fs_stat variable contains a tuple with various information, including block size, total blocks, and free blocks. By multiplying the block size (fs_stat[0]) with the number of free blocks (fs_stat[3]), you can calculate the total free space in bytes.

The code then converts the free space to kilobytes, megabytes, and gigabytes for more convenient reading. Finally, it prints the free space information to the console.

Keep in mind that the code assumes you are working with the root directory (“/”) on the file system. If you want to check the free space in a different directory, you need to modify the path passed to uos.statvfs().

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply