Shortcuts

mmengine.fileio.list_dir_or_file

mmengine.fileio.list_dir_or_file(dir_path, list_dir=True, list_file=True, suffix=None, recursive=False, backend_args=None)[源代码]

Scan a directory to find the interested directories or files in arbitrary order.

备注

list_dir_or_file() returns the path relative to dir_path.

参数:
  • 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.

  • backend_args (dict, optional) – Arguments to instantiate the corresponding backend. Defaults to None.

生成器:

Iterable[str] – A relative path to dir_path.

返回类型:

Iterator[str]

示例

>>> dir_path = '/path/of/dir'
>>> for file_path in list_dir_or_file(dir_path):
...     print(file_path)
>>> # list those files and directories in current directory
>>> for file_path in list_dir_or_file(dir_path):
...     print(file_path)
>>> # only list files
>>> for file_path in list_dir_or_file(dir_path, list_dir=False):
...     print(file_path)
>>> # only list directories
>>> for file_path in list_dir_or_file(dir_path, list_file=False):
...     print(file_path)
>>> # only list files ending with specified suffixes
>>> for file_path in list_dir_or_file(dir_path, suffix='.txt'):
...     print(file_path)
>>> # list all files and directory recursively
>>> for file_path in list_dir_or_file(dir_path, recursive=True):
...     print(file_path)