LocalBackend¶
- class mmeval.fileio.LocalBackend[source]¶
Raw local storage backend.
- exists(filepath: Union[str, pathlib.Path]) → bool[source]¶
Check whether a file path exists.
- Parameters
filepath (str or Path) – Path to be checked whether exists.
- Returns
Return
True
iffilepath
exists,False
otherwise.- Return type
bool
Examples
>>> backend = LocalBackend() >>> filepath = '/path/of/file' >>> backend.exists(filepath) True
- get(filepath: Union[str, pathlib.Path]) → bytes[source]¶
Read bytes from a given
filepath
with ‘rb’ mode.- Parameters
filepath (str or Path) – Path to read data.
- Returns
Expected bytes object.
- Return type
bytes
Examples
>>> backend = LocalBackend() >>> filepath = '/path/of/file' >>> backend.get(filepath) b'hello world'
- get_local_path(filepath: Union[str, pathlib.Path]) → Generator[Union[str, pathlib.Path], None, None][source]¶
Only for unified API and do nothing.
- Parameters
filepath (str or Path) – Path to be read data.
backend_args (dict, optional) – Arguments to instantiate the corresponding backend. Defaults to None.
Examples
>>> backend = LocalBackend() >>> with backend.get_local_path('s3://bucket/abc.jpg') as path: ... # do something here
- get_text(filepath: Union[str, pathlib.Path], encoding: str = 'utf-8') → str[source]¶
Read text from a given
filepath
with ‘r’ mode.- Parameters
filepath (str or Path) – Path to read data.
encoding (str) – The encoding format used to open the
filepath
. Defaults to ‘utf-8’.
- Returns
Expected text reading from
filepath
.- Return type
str
Examples
>>> backend = LocalBackend() >>> filepath = '/path/of/file' >>> backend.get_text(filepath) 'hello world'
- isdir(filepath: Union[str, pathlib.Path]) → bool[source]¶
Check whether a file path is a directory.
- Parameters
filepath (str or Path) – Path to be checked whether it is a directory.
- Returns
Return
True
iffilepath
points to a directory,False
otherwise.- Return type
bool
Examples
>>> backend = LocalBackend() >>> filepath = '/path/of/dir' >>> backend.isdir(filepath) True
- isfile(filepath: Union[str, pathlib.Path]) → bool[source]¶
Check whether a file path is a file.
- Parameters
filepath (str or Path) – Path to be checked whether it is a file.
- Returns
Return
True
iffilepath
points to a file,False
otherwise.- Return type
bool
Examples
>>> backend = LocalBackend() >>> filepath = '/path/of/file' >>> backend.isfile(filepath) True
- join_path(filepath: Union[str, pathlib.Path], *filepaths: Union[str, pathlib.Path]) → str[source]¶
Concatenate all file paths.
Join one or more filepath components intelligently. The return value is the concatenation of filepath and any members of *filepaths.
- Parameters
filepath (str or Path) – Path to be concatenated.
- Returns
The result of concatenation.
- Return type
str
Examples
>>> backend = LocalBackend() >>> filepath1 = '/path/of/dir1' >>> filepath2 = 'dir2' >>> filepath3 = 'path/of/file' >>> backend.join_path(filepath1, filepath2, filepath3) '/path/of/dir/dir2/path/of/file'
- list_dir_or_file(dir_path: Union[str, pathlib.Path], list_dir: bool = True, list_file: bool = True, suffix: Optional[Union[str, Tuple[str]]] = None, recursive: bool = False) → Iterator[str][source]¶
Scan a directory to find the interested directories or files in arbitrary order.
Note
list_dir_or_file()
returns the path relative todir_path
.- Parameters
dir_path (str or Path) – Path of the directory.
list_dir (bool) – List the directories. Defaults to True.
list_file (bool) – List the path of files. Defaults to True.
suffix (str or tuple[str], optional) – File suffix that we are interested in. Defaults to None.
recursive (bool) – If set to True, recursively scan the directory. Defaults to False.
- Yields
Iterable[str] – A relative path to
dir_path
.
Examples
>>> backend = LocalBackend() >>> dir_path = '/path/of/dir' >>> # list those files and directories in current directory >>> for file_path in backend.list_dir_or_file(dir_path): ... print(file_path) >>> # only list files >>> for file_path in backend.list_dir_or_file(dir_path, list_dir=False): ... print(file_path) >>> # only list directories >>> for file_path in backend.list_dir_or_file(dir_path, list_file=False): ... print(file_path) >>> # only list files ending with specified suffixes >>> for file_path in backend.list_dir_or_file(dir_path, suffix='.txt'): ... print(file_path) >>> # list all files and directory recursively >>> for file_path in backend.list_dir_or_file(dir_path, recursive=True): ... print(file_path)