normalize_pathname

pyhelpers.dirs.normalize_pathname(pathname, sep='/', **kwargs)[source]

Converts a pathname to a consistent file path format for cross-platform compatibility.

This function formats a file (or an OS-specific) path to ensure compatibility across Windows, Linux and macOS.

Parameters:
  • pathname (str | bytes | pathlib.Path | os.PathLike) – A pathname.

  • sep (str) – File path separator used by the operating system; defaults to "/" (forward slash) for Linux and macOS pathname.

Returns:

Pathname of a consistent file path format.

Return type:

str

Examples:

>>> from pyhelpers.dirs import normalize_pathname
>>> import os
>>> import pathlib
>>> normalize_pathname("tests\data\dat.csv")
'tests/data/dat.csv'
>>> normalize_pathname("tests//data/dat.csv")
'tests/data/dat.csv'
>>> normalize_pathname(pathlib.Path("tests\data/dat.csv"), sep=os.path.sep)  # On Windows
'tests\data\dat.csv'