Control the storage on a Raspberry Pi Pico device.
| Method | __del__ |
Destructor to ensure SD card is unmounted. |
| Method | __init__ |
Initialize the storage class and mount the SD card. |
| Method | copy |
Copy a file or directory from source_path to destination_path. |
| Method | deserialize |
Write a JSON object to a file. |
| Method | execute |
Run a Python file from the storage. |
| Method | exists |
Check if a file or directory exists. |
| Method | file |
Close an open file handle. |
| Method | file |
Copy an open file to a new location. |
| Method | file |
Move an open file to a new location. |
| Method | file |
Open a file and return the file handle. |
| Method | file |
Read data from an open file. |
| Method | file |
Read data from an open file into a pre-allocated buffer. |
| Method | file |
Seek to a specific position in an open file. |
| Method | file |
Write data to an open file. |
| Method | is |
Check if a path is a directory. |
| Method | listdir |
List files in a directory. |
| Method | mkdir |
Create a new directory. |
| Method | mount |
Mount the SD card. |
| Method | mount |
Mount the SD card as a VFS filesystem. |
| Method | move |
Move a file or directory from source_path to destination_path. |
| Method | read |
Read and return the contents of a file. |
| Method | read |
Read a chunk of data from a file without loading the entire file. |
| Method | read |
Read the contents of a directory and return a list of entries. |
| Method | readinto |
Read data from a file into a pre-allocated buffer. |
| Method | remove |
Remove a file or directory. |
| Method | rename |
Rename a file or directory. |
| Method | rmdir |
Remove a directory. |
| Method | serialize |
Read a file and return its contents as a JSON object. |
| Method | size |
Get the size of a file or directory in bytes. |
| Method | unmount |
Unmount the SD card (including VFS if mounted). |
| Method | unmount |
Unmount the VFS filesystem. |
| Method | write |
Write data to a file, creating or overwriting as needed. |
| Class Variable | __slots__ |
Undocumented |
| Property | active |
Returns True if the storage is active (mounted). |
| Property | vfs |
Returns True if the VFS is mounted (allows use of open(), __import__, etc.). |
| Property | vfs |
Returns the filesystem path prefix for VFS access. |
| Instance Variable | _has |
Undocumented |
| Instance Variable | _vfs |
Undocumented |
Copy a file or directory from source_path to destination_path.
| Parameters | |
sourcestr | The source file or directory path. |
destinationstr | The destination file or directory path. |
bytesint | Bytes per copy chunk. Defaults to 2048. |
| Returns | |
bool | True if the copy succeeded, False otherwise. |
Write a JSON object to a file.
| Parameters | |
jsondict | The JSON object to write. |
filestr | The path of the file to write. |
| Returns | |
bool | True if the write succeeded, False otherwise. |
Run a Python file from the storage.
| Parameters | |
filestr | The path of the script to run. Defaults to "/". |
Check if a file or directory exists.
| Parameters | |
path:str | The path to check. |
| Returns | |
bool | True if the path exists, False otherwise. |
FAT32File, destination_path: str, bytes_per_chunk: int = 2048) -> bool:
¶
Copy an open file to a new location.
| Parameters | |
sourceFAT32File | The open source file handle. |
destinationstr | The destination file path. |
bytesint | Bytes per copy chunk. Defaults to 2048. |
| Returns | |
bool | True if the copy succeeded, False otherwise. |
FAT32File, destination_path: str, bytes_per_chunk: int = 2048) -> bool:
¶
Move an open file to a new location.
| Parameters | |
sourceFAT32File | The open source file handle. |
destinationstr | The destination file path. |
bytesint | Bytes per move chunk. Defaults to 2048. |
| Returns | |
bool | True if the move succeeded, False otherwise. |
Open a file and return the file handle.
| Parameters | |
filestr | The path of the file to open. |
| Returns | |
FAT32File | The open file handle, or None on failure. |
Read data from an open file.
| Parameters | |
fileFAT32File | The open file handle. |
index:int | Starting byte position. Defaults to 0. |
count:int | Number of bytes to read. Defaults to 0. |
decode:bool | Whether to decode as UTF-8. Defaults to True. |
| Returns | |
str or bytes | The read data. |
Read data from an open file into a pre-allocated buffer.
| Parameters | |
fileFAT32File | The open file handle. |
buffer:bytearray | The buffer to read into. |
| Returns | |
int | The number of bytes read. |
Seek to a specific position in an open file.
| Parameters | |
fileFAT32File | The open file handle. |
position:int | The byte position to seek to. |
| Returns | |
bool | True if the seek succeeded, False otherwise. |
Write data to an open file.
| Parameters | |
fileFAT32File | The open file handle. |
data:str or bytes | The data to write. |
mode:str | Write mode ("w", "a", or "wb"). Defaults to "w". |
| Returns | |
bool | True if the write succeeded, False otherwise. |
Check if a path is a directory.
| Parameters | |
path:str | The path to check. |
| Returns | |
bool | True if the path is a directory, False otherwise. |
List files in a directory.
| Parameters | |
path:str | Directory path to list. Defaults to "". |
| Returns | |
list[str] | The filenames in the directory. |
Create a new directory.
| Parameters | |
path:str | The path of the directory to create. |
| Returns | |
bool | True if the directory was created, False otherwise. |
Mount the SD card as a VFS filesystem.
This enables the use of Python's built-in open(), __import__, and os module functions with paths on the SD card.
Example
storage = Storage() storage.mount_vfs("/sd")
- with open("/sd/myfile.txt", "r") as f:
- content = f.read()
import sys sys.path.append("/sd/picoware/apps") import myapp
| Parameters | |
mountstr | The mount point path. Defaults to "/sd". |
| Returns | |
bool | True if mounted successfully, False otherwise. |
Move a file or directory from source_path to destination_path.
| Parameters | |
sourcestr | The source file or directory path. |
destinationstr | The destination file or directory path. |
| Returns | |
bool | True if the move succeeded, False otherwise. |
Read and return the contents of a file.
| Parameters | |
filestr | The path of the file to read. |
mode:str | Read mode ("r" for text, otherwise binary). Defaults to "r". |
index:int | Starting byte position. Defaults to 0. |
count:int | Number of bytes to read. Defaults to 0. |
| Returns | |
str or bytes | The file contents. |
Read a chunk of data from a file without loading the entire file.
| Parameters | |
filestr | Path to the file to read. |
start:int | Starting byte position (offset) in the file. Defaults to 0. |
chunkint | Number of bytes to read from the start position. Defaults to 1024. |
| Returns | |
bytes | The chunk of data read from the file. |
Read the contents of a directory and return a list of entries.
Each entry is a dictionary with keys: filename, size, date, time, attributes, and is_directory.
| Parameters | |
path:str | The directory path to read. Defaults to "". |
| Returns | |
list[dict] | The directory entries, or an empty list on failure. |
Read data from a file into a pre-allocated buffer.
| Parameters | |
filestr | The path of the file to read. |
buffer:bytearray | The buffer to read into. |
| Returns | |
int | The number of bytes read. |
Remove a file or directory.
| Parameters | |
filestr | The path of the file or directory to remove. |
| Returns | |
bool | True if removed successfully, False otherwise. |
Rename a file or directory.
| Parameters | |
oldstr | The current path. |
newstr | The new path. |
| Returns | |
bool | True if renamed successfully, False otherwise. |
Remove a directory.
| Parameters | |
path:str | The path of the directory to remove. |
| Returns | |
bool | True if removed successfully, False otherwise. |
Read a file and return its contents as a JSON object.
| Parameters | |
filestr | The path of the file to read. |
| Returns | |
dict | The parsed JSON object, or an empty dict on failure. |
Get the size of a file or directory in bytes.
| Parameters | |
filestr | The path to measure. |
| Returns | |
int | The size in bytes. |
Unmount the SD card (including VFS if mounted).
| Returns | |
bool | True if unmounted successfully, False otherwise. |
Unmount the VFS filesystem.
| Parameters | |
mountstr | The mount point path. Defaults to "/sd". |
| Returns | |
bool | True if unmounted successfully, False otherwise. |
Write data to a file, creating or overwriting as needed.
| Parameters | |
filestr | The path of the file to write. |
data:str | The data to write. |
mode:str | Write mode ("w" to overwrite, "a" to append). Defaults to "w". |
| Returns | |
bool | True if the write succeeded, False otherwise. |