Sdcard remounting and checking availability

Not sure if this is the right place to post this but I was wondering if anyone knew the best way to check if an SD card is inserted and also how to remount the SD card if it was removed and re-inserted?

on a previous build (lv micropython) I was able to mount an SD card again after de-initializing the SPI bus.

Now I get the following error:

MicroPython v1.15-835-g1fcd6e97f-dirty on 2021-08-21; ESP32 module (spiram) with ESP32
Type "help()" for more information.
>>> import uos
>>> from machine import SDCard
>>> sd = SDCard(slot=2)
>>> uos.mount(sd, '/sd')
>>> uos.listdir('/')
['sd', '']
>>> uos.umount('/sd')
>>> sd.deinit()
>>> sd = SDCard(slot=2)
E (145181) spi: spi_bus_initialize(462): SPI bus already initialized.
E (145181) sdspi_host: spi_bus_initialize failed with rc=0x103
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>

I would just like to handle the use case where some one removes an SD card then re-inserts it again.

Also it seems like even after an SD card is removed some files are still readable. For now the best way I know how to check if the SD card is inserted is to create file on mount that that I can write to, to validate the SD card is still accessible.

            f = open('/sd/.avail', 'w')

Any help would be greatly appreciated :slight_smile:

Looks like the deinit issue got fixed:

But the “reinit” is still ongoing: