In python, why is os.path.splitext
using '.' as extension separator instead of os.extsep
?
splitext() method in Python is used to split the path name into a pair root and ext. Here, ext stands for extension and has the extension portion of the specified path while root is everything except ext part.
splitext() is a built-in Python function that splits the pathname into the pair of root and ext. The ext stands for extension and has the extension portion of the specified path, while the root is everything except the ext part. Everything before the final slash and everything after it.
split() method in Python is used to Split the path name into a pair head and tail. Here, tail is the last path name component and head is everything leading up to that.
The __file__ variable: __file__ is a variable that contains the path to the module that is currently being imported. Python creates a __file__ variable for itself when it is about to import a module.
os.extsep
is defined by importing os.path.extsep
. But you're right, os.path.splitext()
always uses .
, regardless of os.path.extsep
:
From os.py
(3.2.2):
from os.path import (curdir, pardir, sep, pathsep, defpath, extsep, altsep,
devnull)
From ntpath.py
(which becomes os.path
)
extsep = '.'
[...]
def _get_dot(path):
if isinstance(path, bytes):
return b'.'
else:
return '.' # instead of return extsep! [Comment by me, not in source]
[...]
def splitext(p):
return genericpath._splitext(p, _get_sep(p), _get_altsep(p),
_get_dot(p))
Also, from genericpath.py
:
def _get_dot(path):
if isinstance(path, bytes):
return b'.'
else:
return '.'
So os.path()
does in fact define the extension separator twice.
Now it probably doesn't matter because it's not going to change anytime soon (it's the same on all supported platforms anyway). But in a way, it violates the DRY principle.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With