list_file_names()

audeer.list_file_names(path, *, filetype='', basenames=False, recursive=False, hidden=False)[source]

List of file names inferred from provided path.

Parameters
  • path (Union[str, bytes]) – path to directory, or path to directory plus file matching pattern, e.g. 'dir/file.txt'. If recursive is True returns all files named file.txt from all sub-folders. Besides the filename *, ?, [seq], and [!seq] can be used as pattern, compare fnmatch

  • filetype (str) – optional consider only this filetype

  • basenames (bool) – if True return relative path in respect to path

  • recursive (bool) – if True includes subdirectories

  • hidden (bool) – if True includes files starting with a dot (.)

Return type

List[str]

Returns

alphabetically sorted list of path(s) to file(s)

Raises

NotADirectoryError – if path is a directory or file that does not exist, or path is a pattern and os.dirname(path) does not exist

Examples

>>> dir_path = mkdir("path")
>>> _ = touch(dir_path, "file.wav")
>>> _ = touch(dir_path, "File.wav")
>>> _ = touch(dir_path, ".lock")
>>> sub_dir_path = mkdir("path", "sub")
>>> _ = touch(sub_dir_path, "file.ogg")
>>> _ = touch(sub_dir_path, ".lock")
>>> list_file_names(
...     dir_path,
...     basenames=True,
... )
['File.wav', 'file.wav']
>>> list_file_names(
...     dir_path,
...     basenames=True,
...     hidden=True,
... )
['.lock', 'File.wav', 'file.wav']
>>> list_file_names(
...     dir_path,
...     basenames=True,
...     recursive=True,
... )
['File.wav', 'file.wav', 'sub/file.ogg']
>>> list_file_names(
...     dir_path,
...     basenames=True,
...     recursive=True,
...     hidden=True,
... )
['.lock', 'File.wav', 'file.wav', 'sub/.lock', 'sub/file.ogg']
>>> list_file_names(
...     os.path.join(dir_path, "f*"),
...     basenames=True,
...     recursive=True,
...     hidden=True,
... )
['file.wav', 'sub/file.ogg']
>>> list_file_names(
...     os.path.join(dir_path, "[fF]*"),
...     basenames=True,
...     recursive=True,
...     hidden=True,
... )
['File.wav', 'file.wav', 'sub/file.ogg']
>>> list_file_names(
...     os.path.join(dir_path, "[!f]*"),
...     basenames=True,
...     recursive=True,
...     hidden=True,
... )
['.lock', 'File.wav', 'sub/.lock']
>>> list_file_names(
...     os.path.join(dir_path, "f*"),
...     filetype="ogg",
...     basenames=True,
...     recursive=True,
...     hidden=True,
... )
['sub/file.ogg']