Search code examples
python-3.xlinuxwindowscross-platform

differences in python path handling with trailing dot ('.') between linux and windows


I came across a surprising result while fixing for a cross-platform bug in a python script. (sidenote: same venv specified by poetry, otoh this is basic python library functionality)

wanting to reduce some path-trunk by a header part, i used:
os.path.relpath('../data/what/ever/path/trunk.', '../data/')
which for linux yields:
'what/ever/path/trunk.'
while windows produces:
'what/ever/path/trunk' (mark the missing dot at the end)

why is this, and how can such aberrant behavior be justified ?


Solution

  • On Windows, the last . in a filename is used to separate the file extension from the rest of the filename. If the extension is blank, the . is optional; both forms would reference the same file.

    Linux doesn't have filename extensions, so the trailing . is a valid part of the filename and must be preserved.