nice recurisve file lister
This commit is contained in:
parent
b284c3b0eb
commit
bc357671ee
1 changed files with 49 additions and 0 deletions
49
Introducing_Trinket_M0/Trinket_SDCardList.py
Normal file
49
Introducing_Trinket_M0/Trinket_SDCardList.py
Normal file
|
|
@ -0,0 +1,49 @@
|
||||||
|
import adafruit_sdcard
|
||||||
|
import busio
|
||||||
|
import digitalio
|
||||||
|
import board
|
||||||
|
import storage
|
||||||
|
import os
|
||||||
|
|
||||||
|
# Use any pin that is not taken by SPI
|
||||||
|
SD_CS = board.D0
|
||||||
|
|
||||||
|
# Connect to the card and mount the filesystem.
|
||||||
|
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
|
||||||
|
cs = digitalio.DigitalInOut(SD_CS)
|
||||||
|
sdcard = adafruit_sdcard.SDCard(spi, cs)
|
||||||
|
vfs = storage.VfsFat(sdcard)
|
||||||
|
storage.mount(vfs, "/sd")
|
||||||
|
|
||||||
|
# Use the filesystem as normal! Our files are under /sd
|
||||||
|
|
||||||
|
# This helper function will print the contents of the SD
|
||||||
|
def print_directory(path, tabs = 0):
|
||||||
|
for file in os.listdir(path):
|
||||||
|
stats = os.stat(path+"/"+file)
|
||||||
|
filesize = stats[6]
|
||||||
|
isdir = stats[0] & 0x4000
|
||||||
|
|
||||||
|
if filesize < 1000:
|
||||||
|
sizestr = str(filesize) + " by"
|
||||||
|
elif filesize < 1000000:
|
||||||
|
sizestr = "%0.1f KB" % (filesize/1000)
|
||||||
|
else:
|
||||||
|
sizestr = "%0.1f MB" % (filesize/1000000)
|
||||||
|
|
||||||
|
prettyprintname = ""
|
||||||
|
for i in range(tabs):
|
||||||
|
prettyprintname += " "
|
||||||
|
prettyprintname += file
|
||||||
|
if isdir:
|
||||||
|
prettyprintname += "/"
|
||||||
|
print('{0:<40} Size: {1:>10}'.format(prettyprintname, sizestr))
|
||||||
|
|
||||||
|
# recursively print directory contents
|
||||||
|
if isdir:
|
||||||
|
print_directory(path+"/"+file, tabs+1)
|
||||||
|
|
||||||
|
|
||||||
|
print("Files on filesystem:")
|
||||||
|
print("====================")
|
||||||
|
print_directory("/sd")
|
||||||
Loading…
Reference in a new issue