path()

audeer.path(path, *paths, follow_symlink=False, expand_vars=False)[source]

Expand and normalize to absolute path.

It uses os.path.realpath() and os.path.expanduser() to ensure an absolute path without .. or ~, and independent of the path separator of the operating system. If follow_symlink is False, the faster os.path.abspath() is used instead of os.path.realpath(). If expand_vars is True, environment variables are expanded by os.path.expandvars().

Parameters:
  • path (str | bytes) – path to file, directory

  • *paths (Sequence[str | bytes]) – additional arguments to be joined with path by os.path.join()

  • follow_symlink (bool) – if True symlinks are followed and the path of the original file is returned

  • expand_vars (bool) – if True environment variables of the form $name or ${name} (and %name% under Windows) are expanded. Undefined variables are left unchanged

Return type:

str

Returns:

(joined and) expanded path

Examples

>>> home = audeer.path("~")
>>> folder = audeer.path("~/path/.././path")
>>> folder[len(home) + 1 :]
'path'
>>> file = audeer.path("~/path/.././path", "./file.txt")
>>> file[len(home) + 1 :]
'path/file.txt'
>>> file = audeer.touch("file.txt")
>>> link = audeer.path("link.txt")
>>> os.symlink(file, link)
>>> os.path.basename(audeer.path(link))
'link.txt'
>>> os.path.basename(audeer.path(link, follow_symlink=True))
'file.txt'
>>> os.environ["DATA_ROOT"] = "~/data"
>>> file = audeer.path("$DATA_ROOT/a.wav", expand_vars=True)
>>> file[len(home) + 1 :]
'data/a.wav'
>>> del os.environ["DATA_ROOT"]