maix.fs

maix.fs module

You can use maix.fs to access this module with MaixPy
This module is generated from MaixPy and MaixCDK

Module

No module

Enum

SEEK

SEEK enums

item describe
values FS_SEEK_SET: Seek from beginning of file.
FS_SEEK_CUR: Seek from current position.
FS_SEEK_END: Seek from end of file.

C++ defination code:

enum class SEEK
    {
        FS_SEEK_SET = 0,  // Seek from beginning of file.
        FS_SEEK_CUR = 1,  // Seek from current position.
        FS_SEEK_END = 2,  // Seek from end of file.
    }

Variable

Function

isabs

def isabs(path: str) -> bool

Check if the path is absolute path

item description
param path: path to check
return true if path is absolute path

C++ defination code:

bool isabs(const std::string &path)

isdir

def isdir(path: str) -> bool

Check if the path is a directory, if not exist, throw exception

item description
param path: path to check
return true if path is a directory

C++ defination code:

bool isdir(const std::string &path)

isfile

def isfile(path: str) -> bool

Check if the path is a file, if not exist, throw exception

item description
param path: path to check
return true if path is a file

C++ defination code:

bool isfile(const std::string &path)
def islink(path: str) -> bool

Check if the path is a link, if not exist, throw exception

item description
param path: path to check
return true if path is a link

C++ defination code:

bool islink(const std::string &path)
def symlink(src: str, link: str, force: bool = False) -> maix.err.Err

Create soft link

item description
param src: real file path
link: link file path
force: force link, if already have link file, will delet it first then create.

C++ defination code:

err::Err symlink(const std::string &src, const std::string &link, bool force = false)

exists

def exists(path: str) -> bool

Check if the path exists

item description
param path: path to check
return true if path exists

C++ defination code:

bool exists(const std::string &path)

mkdir

def mkdir(path: str, exist_ok: bool = True, recursive: bool = True) -> maix.err.Err

Create a directory recursively

item description
param path: path to create
exist_ok: if true, also return true if directory already exists
recursive: if true, create directory recursively, otherwise, only create one directory, default is true
return err::ERR_NONE(err.Err.ERR_NONE in MaixPy) if success, other error code if failed

C++ defination code:

err::Err mkdir(const std::string &path, bool exist_ok = true, bool recursive = true)

rmdir

def rmdir(path: str, recursive: bool = False) -> maix.err.Err

Remove a directory

item description
param path: path to remove
recursive: if true, remove directory recursively, otherwise, only remove empty directory, default is false
return err::ERR_NONE(err.Err.ERR_NONE in MaixPy) if success, other error code if failed

C++ defination code:

err::Err rmdir(const std::string &path, bool recursive = false)

remove

def remove(path: str) -> maix.err.Err

Remove a file

item description
param path: path to remove
return err::ERR_NONE(err.Err.ERR_NONE in MaixPy) if success, other error code if failed

C++ defination code:

err::Err remove(const std::string &path)

rename

def rename(src: str, dst: str) -> maix.err.Err

Rename a file or directory

item description
param src: source path
dst: destination path, if destination dirs not exist, will auto create
return err::ERR_NONE(err.Err.ERR_NONE in MaixPy) if success, other error code if failed

C++ defination code:

err::Err rename(const std::string &src, const std::string &dst)

sync

def sync() -> None

Sync files, ensure they're wrriten to disk from RAM

C++ defination code:

void sync()

getsize

def getsize(path: str) -> int

Get file size

item description
param path: path to get size
return file size if success, -err::Err code if failed

C++ defination code:

int getsize(const std::string &path)

dirname

def dirname(path: str) -> str

Get directory name of path

item description
param path: path to get dirname
return dirname if success, empty string if failed

C++ defination code:

std::string dirname(const std::string &path)

basename

def basename(path: str) -> str

Get base name of path

item description
param path: path to get basename
return basename if success, empty string if failed

C++ defination code:

std::string basename(const std::string &path)

abspath

def abspath(path: str) -> str

Get absolute path

item description
param path: path to get absolute path
return absolute path if success, empty string if failed

C++ defination code:

std::string abspath(const std::string &path)

getcwd

def getcwd() -> str

Get current working directory

item description
return current working directory absolute path

C++ defination code:

std::string getcwd()

realpath

def realpath(path: str) -> str

Get realpath of path

item description
param path: path to get realpath
return realpath if success, empty string if failed

C++ defination code:

std::string realpath(const std::string &path)

splitext

def splitext(path: str) -> list[str]

Get file extension

item description
param path: path to get extension
return prefix_path and extension list if success, empty string if failed

C++ defination code:

std::vector<std::string> splitext(const std::string &path)

listdir

def listdir(path: str, recursive: bool = False, full_path: bool = False) -> list[str]

List files in directory

item description
param path: path to list
recursive: if true, list recursively, otherwise, only list current directory, default is false
full_path: if true, return full path, otherwise, only return basename, default is false
return files list if success, nullptr if failed, you should manually delete it in C++.

C++ defination code:

std::vector<std::string> *listdir(const std::string &path, bool recursive = false, bool full_path = false)

join

def join(paths: list[str]) -> str

Join paths

item description
param paths: paths to join
return joined path if success, empty string if failed

C++ defination code:

std::string join(const std::vector<std::string> &paths)

open

def open(path: str, mode: str) -> File

Open a file, and return a File object

item description
param path: path to open
mode: open mode, support "r", "w", "a", "r+", "w+", "a+", "rb", "wb", "ab", "rb+", "wb+", "ab+"
return File object if success(need to delete object manually in C/C++), nullptr if failed

C++ defination code:

fs::File *open(const std::string &path, const std::string &mode)

tempdir

def tempdir() -> str

Get temp files directory

item description
return temp files directory

C++ defination code:

std::string tempdir()

Class

File

File read write ops

C++ defination code:

class File

__init__

def __init__(self) -> None

Construct File object

item description
type func
static False

C++ defination code:

File()

open

def open(self, path: str, mode: str) -> maix.err.Err

Open a file

item description
type func
param path: path to open
mode: open mode, support "r", "w", "a", "r+", "w+", "a+", "rb", "wb", "ab", "rb+", "wb+", "ab+"
return err::ERR_NONE(err.Err.ERR_NONE in MaixPy) if success, other error code if failed
static False

C++ defination code:

err::Err open(const std::string &path, const std::string &mode)

close

def close(self) -> None

Close a file.

item description
type func
attention not ensure data is written to disk, you can use sync() before close to ensure data is written to disk.
static False

C++ defination code:

void close()

read

def read(self, size: int) -> list[int]

Read data from file API2

item description
type func
param size: max read size
return bytes data if success(need delete manually in C/C++), nullptr if failed
static False

C++ defination code:

std::vector<uint8_t> *read(int size)

readline

def readline(self) -> str

Read line from file

item description
type func
return line if success, None(nullptr in C++) if failed. You need to delete the returned object manually in C/C++.
static False

C++ defination code:

std::string *readline()

eof

def eof(self) -> int

End of file or not

item description
type func
return 0 if not reach end of file, else eof.
static False

C++ defination code:

int eof()

write

def write(self, buf: list[int]) -> int

Write data to file API2

item description
type func
param buf: buffer to write
attention will not ensure data is flush to kernel or written to disk, you can use flush() to flush data to kernel,
and sync() to ensure data is written to disk.
return write size if success, -err::Err code if failed
static False

C++ defination code:

int write(const std::vector<uint8_t> &buf)

seek

def seek(self, offset: int, whence: int) -> int

Seek file position

item description
type func
param offset: offset to seek
whence: @see maix.fs.SEEK
return new position if success, -err::Err code if failed
static False

C++ defination code:

int seek(int offset, int whence)

tell

def tell(self) -> int

Get file position

item description
type func
return file position if success, -err::Err code if failed
static False

C++ defination code:

int tell()

size

def size(self) -> int

Get file size

item description
type func
attention will change file seek position temporarily when get file size.
return file size if success, -err::Err code if failed
static False

C++ defination code:

int size()

flush

def flush(self) -> maix.err.Err

Flush file, ensure data is written to kernel buffer.

item description
type func
attention not ensure data is written to disk, use sync() to ensure data is written to disk.
return err::ERR_NONE(err.Err.ERR_NONE in MaixPy) if success, other error code if failed
static False

C++ defination code:

err::Err flush()

sync

def sync(self) -> maix.err.Err

Sync file, ensure data is written to disk.

item description
type func
return err::ERR_NONE(err.Err.ERR_NONE in MaixPy) if success, other error code if failed
static False

C++ defination code:

err::Err sync()