API Documentation

littlefs module

class littlefs.FileHandle(fs, fh)
close()

Flush and close the IO object.

This method has no effect if the file is already closed.

flush()

Flush write buffers, if applicable.

This is not implemented for read-only and non-blocking streams.

readable()

Return whether object was opened for reading.

If False, read() will raise OSError.

readall()

Read until EOF, using multiple read() call.

readinto(b)
seek(offset, whence=0)

Change the stream position to the given byte offset.

offset

The stream position, relative to ‘whence’.

whence

The relative position to seek from.

The offset is interpreted relative to the position indicated by whence. Values for whence are:

  • os.SEEK_SET or 0 – start of stream (the default); offset should be zero or positive

  • os.SEEK_CUR or 1 – current stream position; offset may be negative

  • os.SEEK_END or 2 – end of stream; offset is usually negative

Return the new absolute position.

seekable()

Return whether object supports random access.

If False, seek(), tell() and truncate() will raise OSError. This method may need to do a test seek().

tell()

Return current stream position.

truncate(size=None) int

Truncate file to size bytes.

File pointer is left unchanged. Size defaults to the current IO position as reported by tell(). Returns the new size.

writable()

Return whether object was opened for writing.

If False, write() will raise OSError.

write(data)
class littlefs.LFSConfig(context=None, int block_size: int = 128, *, int block_count: int = 64, int read_size: int = 0, int prog_size: int = 0, int block_cycles: int = -1, int cache_size: int = 0, int lookahead_size: int = 8, int name_max: int = 255, int file_max: int = 0, int attr_max: int = 0, int metadata_max: int = 0, int disk_version: int = 0)
attr_max
block_count
block_size
cache_size
disk_version
file_max
lookahead_size
metadata_max
name_max
prog_size
read_size
class littlefs.LFSDirectory
class littlefs.LFSFSStat(disk_version: int, name_max: int, file_max: int, attr_max: int, block_count: int, block_size: int)

Littlefs filesystem status.

attr_max: int

Alias for field number 3

block_count: int

Alias for field number 4

block_size: int

Alias for field number 5

disk_version: int

Alias for field number 0

file_max: int

Alias for field number 2

name_max: int

Alias for field number 1

class littlefs.LFSFile
flags

LFSFileFlag Mode flags of an open file

Type:

LFSFile.flags

class littlefs.LFSFileFlag(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)

Littlefs file mode flags

append = 2048
creat = 256
excl = 512
rdonly = 1
rdwr = 3
trunc = 1024
wronly = 2
class littlefs.LFSFilesystem
block_count

lfs_size_t

Type:

LFSFilesystem.block_count

class littlefs.LFSStat(type: int, size: int, name: str)

Littlefs File / Directory status.

TYPE_DIR = 2
TYPE_REG = 1
name: str

Alias for field number 2

size: int

Alias for field number 1

type: int

Alias for field number 0

class littlefs.LittleFS(context: UserContext | None = None, mount=True, **kwargs)

Littlefs file system

property block_count: int
property context: UserContext

User context of the file system

format() int

Format the underlying buffer

fs_grow(block_count: int) int
fs_mkconsistent() int

Attempt to make the filesystem consistent and ready for writing

fs_stat() LFSFSStat

Get the status of the filesystem

getattr(path: str, typ: str | bytes | int) bytes
listdir(path='.') List[str]

List directory content

List the content of a directory. This function uses scandir() internally. Using scandir() might be better if you are searching for a specific file or need access to the littlefs.lfs.LFSStat of the files.

makedirs(name: str, exist_ok=False)

Recursive directory creation function.

mkdir(path: str) int

Create a new directory

mount() int

Mount the underlying buffer

open(fname: str, mode='r', buffering: int = -1, encoding: str = None, errors: str = None, newline: str = None) IO

Open a file.

mode is an optional string that specifies the mode in which the file is opened and is analogous to the built-in io.open() function. Files opened in text mode (default) will take and return str objects. Files opened in binary mode will take and return byte-like objects.

Parameters:
  • fname (str) – The path to the file to open.

  • mode (str) – Specifies the mode in which the file is opened.

  • buffering (int) – Specifies the buffering policy. Pass 0 to disable buffering in binary mode.

  • encoding (str) – Text encoding to use. (text mode only)

  • errors (str) – Specifies how encoding and decoding errors are to be handled. (text mode only)

  • newline (str) – Controls how universal newlines mode works. (text mode only)

remove(path: str, recursive: bool = False) None

Remove a file or directory

If the path to remove is a directory, the directory must be empty.

Parameters:
  • path (str) – The path to the file or directory to remove.

  • recursive (bool) – If true and path is a directory, recursively remove all children files/folders.

removeattr(path: str, typ: str | bytes | int) None
removedirs(name)

Remove directories recursively

This works like remove() but if the leaf directory is empty after the successful removal of name, the function tries to recursively remove all parent directories which are also empty.

rename(src: str, dst: str) int

Rename a file or directory

rmdir(path: str) int

Remove a directory

This function is an alias for remove()

scandir(path='.') Iterator[LFSStat]

List directory content

setattr(path: str, typ: str | bytes | int, data: bytes) None
stat(path: str) LFSStat

Get the status of a file or directory

Remove a file or directory

This function is an alias for remove().

unmount() int

Unmount the underlying buffer

walk(top: str) Iterator[Tuple[str, List[str], List[str]]]

Generate the file names in a directory tree

Generate the file and directory names in a directory tree by walking the tree top-down. This functions closely resembels the behaviour of os.walk().

Each iteration yields a tuple containing three elements:

  • The root of the currently processed element

  • A list of directories located in the root

  • A list of filenames located in the root

exception littlefs.LittleFSError(code: int)
class Error(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)
LFS_ERR_BADF = -9
LFS_ERR_CORRUPT = -84
LFS_ERR_EXIST = -17
LFS_ERR_FBIG = -27
LFS_ERR_INVAL = -22
LFS_ERR_IO = -5
LFS_ERR_ISDIR = -21
LFS_ERR_NAMETOOLONG = -36
LFS_ERR_NOATTR = -61
LFS_ERR_NOENT = -2
LFS_ERR_NOMEM = -12
LFS_ERR_NOSPC = -28
LFS_ERR_NOTDIR = -20
LFS_ERR_NOTEMPTY = -39
LFS_ERR_OK = 0
property name: str
class littlefs.UserContext(buffsize: int)

Basic User Context Implementation

erase(cfg: LFSConfig, block: int) int

Erase a block

Parameters:
  • cfg (LFSConfig) – Filesystem configuration object

  • block (int) – Block number to read

prog(cfg: LFSConfig, block: int, off: int, data: bytes) int

program data

Parameters:
  • cfg (LFSConfig) – Filesystem configuration object

  • block (int) – Block number to program

  • off (int) – Offset from start of block

  • data (bytes) – Data to write

read(cfg: LFSConfig, block: int, off: int, size: int) bytearray

read data

Parameters:
  • cfg (LFSConfig) – Filesystem configuration object

  • block (int) – Block number to read

  • off (int) – Offset from start of block

  • size (int) – Number of bytes to read.

sync(cfg: LFSConfig) int

Sync cached data

Parameters:

cfg (LFSConfig) – Filesystem configuration object

class littlefs.UserContextWinDisk(disk_path: str)

Windows disk/file context

erase(cfg: LFSConfig, block: int) int

Erase a block

Parameters:
  • cfg (LFSConfig) – Filesystem configuration object

  • block (int) – Block number to read

prog(cfg: LFSConfig, block: int, off: int, data: bytes) int

program data

Parameters:
  • cfg (LFSConfig) – Filesystem configuration object

  • block (int) – Block number to program

  • off (int) – Offset from start of block

  • data (bytes) – Data to write

read(cfg: LFSConfig, block: int, off: int, size: int) bytearray

read data

Parameters:
  • cfg (LFSConfig) – Filesystem configuration object

  • block (int) – Block number to read

  • off (int) – Offset from start of block

  • size (int) – Number of bytes to read.

sync(cfg: LFSConfig) int

Sync cached data

Parameters:

cfg (LFSConfig) – Filesystem configuration object

littlefs.context module

class littlefs.context.UserContext(buffsize: int)

Basic User Context Implementation

erase(cfg: LFSConfig, block: int) int

Erase a block

Parameters:
  • cfg (LFSConfig) – Filesystem configuration object

  • block (int) – Block number to read

prog(cfg: LFSConfig, block: int, off: int, data: bytes) int

program data

Parameters:
  • cfg (LFSConfig) – Filesystem configuration object

  • block (int) – Block number to program

  • off (int) – Offset from start of block

  • data (bytes) – Data to write

read(cfg: LFSConfig, block: int, off: int, size: int) bytearray

read data

Parameters:
  • cfg (LFSConfig) – Filesystem configuration object

  • block (int) – Block number to read

  • off (int) – Offset from start of block

  • size (int) – Number of bytes to read.

sync(cfg: LFSConfig) int

Sync cached data

Parameters:

cfg (LFSConfig) – Filesystem configuration object

class littlefs.context.UserContextWinDisk(disk_path: str)

Windows disk/file context

erase(cfg: LFSConfig, block: int) int

Erase a block

Parameters:
  • cfg (LFSConfig) – Filesystem configuration object

  • block (int) – Block number to read

prog(cfg: LFSConfig, block: int, off: int, data: bytes) int

program data

Parameters:
  • cfg (LFSConfig) – Filesystem configuration object

  • block (int) – Block number to program

  • off (int) – Offset from start of block

  • data (bytes) – Data to write

read(cfg: LFSConfig, block: int, off: int, size: int) bytearray

read data

Parameters:
  • cfg (LFSConfig) – Filesystem configuration object

  • block (int) – Block number to read

  • off (int) – Offset from start of block

  • size (int) – Number of bytes to read.

sync(cfg: LFSConfig) int

Sync cached data

Parameters:

cfg (LFSConfig) – Filesystem configuration object

littlefs.lfs module

class littlefs.lfs.LFSStat(type: int, size: int, name: str)

Littlefs File / Directory status.

name: str

Alias for field number 2

size: int

Alias for field number 1

type: int

Alias for field number 0

class littlefs.lfs.LFSConfig(context=None, int block_size: int = 128, *, int block_count: int = 64, int read_size: int = 0, int prog_size: int = 0, int block_cycles: int = -1, int cache_size: int = 0, int lookahead_size: int = 8, int name_max: int = 255, int file_max: int = 0, int attr_max: int = 0, int metadata_max: int = 0, int disk_version: int = 0)
attr_max
block_count
block_size
cache_size
disk_version
file_max
lookahead_size
metadata_max
name_max
prog_size
read_size
class littlefs.lfs.LFSDirectory
class littlefs.lfs.LFSFSStat(disk_version: int, name_max: int, file_max: int, attr_max: int, block_count: int, block_size: int)

Littlefs filesystem status.

attr_max: int

Alias for field number 3

block_count: int

Alias for field number 4

block_size: int

Alias for field number 5

disk_version: int

Alias for field number 0

file_max: int

Alias for field number 2

name_max: int

Alias for field number 1

class littlefs.lfs.LFSFile
flags

LFSFileFlag Mode flags of an open file

Type:

LFSFile.flags

class littlefs.lfs.LFSFileFlag(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)

Littlefs file mode flags

append = 2048
creat = 256
excl = 512
rdonly = 1
rdwr = 3
trunc = 1024
wronly = 2
class littlefs.lfs.LFSFilesystem
block_count

lfs_size_t

Type:

LFSFilesystem.block_count

class littlefs.lfs.LFSStat(type: int, size: int, name: str)

Littlefs File / Directory status.

TYPE_DIR = 2
TYPE_REG = 1
name: str

Alias for field number 2

size: int

Alias for field number 1

type: int

Alias for field number 0

littlefs.lfs.dir_close(LFSFilesystem fs, LFSDirectory dh)
littlefs.lfs.dir_open(LFSFilesystem fs, path)
littlefs.lfs.dir_read(LFSFilesystem fs, LFSDirectory dh)
littlefs.lfs.dir_rewind(LFSFilesystem fs, LFSDirectory dh)
littlefs.lfs.dir_tell(LFSFilesystem fs, LFSDirectory dh)
littlefs.lfs.file_close(LFSFilesystem fs, LFSFile fh)
littlefs.lfs.file_open(LFSFilesystem fs, path, flags)
littlefs.lfs.file_open_cfg(self, path, flags, config)
littlefs.lfs.file_read(LFSFilesystem fs, LFSFile fh, size)
littlefs.lfs.file_rewind(LFSFilesystem fs, LFSFile fh)
littlefs.lfs.file_seek(LFSFilesystem fs, LFSFile fh, off, whence)
littlefs.lfs.file_size(LFSFilesystem fs, LFSFile fh)
littlefs.lfs.file_sync(LFSFilesystem fs, LFSFile fh)
littlefs.lfs.file_tell(LFSFilesystem fs, LFSFile fh)
littlefs.lfs.file_truncate(LFSFilesystem fs, LFSFile fh, size)
littlefs.lfs.file_write(LFSFilesystem fs, LFSFile fh, data)
littlefs.lfs.format(LFSFilesystem fs, LFSConfig cfg)

Format the filesystem

littlefs.lfs.fs_grow(LFSFilesystem fs, block_count) int

Irreversibly grows the filesystem to a new size.

Parameters:
  • fs (LFSFilesystem)

  • block_count (int) – Number of blocks in the new filesystem.

littlefs.lfs.fs_mkconsistent(LFSFilesystem fs)

Attempt to make the filesystem consistent and ready for writing

littlefs.lfs.fs_size(LFSFilesystem fs)
littlefs.lfs.fs_stat(LFSFilesystem fs)

Get filesystem status

littlefs.lfs.getattr(LFSFilesystem fs, path, typ)
littlefs.lfs.mkdir(LFSFilesystem fs, path)
littlefs.lfs.mount(LFSFilesystem fs, LFSConfig cfg)

Mount the filesystem

littlefs.lfs.remove(LFSFilesystem fs, path)

Remove a file or directory

If removing a directory, the directory must be empty.

littlefs.lfs.removeattr(LFSFilesystem fs, path, typ)
littlefs.lfs.rename(LFSFilesystem fs, oldpath, newpath)

Rename or move a file or directory

If the destination exists, it must match the source in type. If the destination is a directory, the directory must be empty.

littlefs.lfs.setattr(LFSFilesystem fs, path, typ, data)
littlefs.lfs.stat(LFSFilesystem fs, path)

Find info about a file or directory

littlefs.lfs.unmount(LFSFilesystem fs)

Unmount the filesystem

This does nothing beside releasing any allocated resources