Don't join paths yourself. The standard library comes both with the os.path
and pathlib
modules, which abstract away (most) platform differences.
For example, getting the current module directory is:
import os.path
source_path = os.path.dirname(os.path.abspath(__file__))
or
import pathlib
source_path = pathlib.Path(__file__).resolve().parent
both of which give you the appropriate absolute path for the current platform.
There is also the os.sep
value, which is the main directory separator character for the current platform, as well as os.altsep
for platforms such as Windows where multiple path separator characters can be used.
The standard library versions can be relied upon to handle platform-specific edge-cases, such as mixed forward and backward slashes, drive names and UNC paths on Windows.