In this other question, the votes clearly show that the os.path.splitext function is preferred over the simple .split('.')[-1] string manipulation. Does anyone have a moment to explain exactly why that is? Is it faster, or more accurate, or what? I’m willing to accept that there’s something better about it, but I can’t immediately see what it might be. Might importing a whole module to do this be overkill, at least in simple cases?
EDIT: The OS specificity is a big win that’s not immediately obvious; but even I should’ve seen the ‘what if there isn’t a dot’ case! And thanks to everybody for the general comments on library usage.
Well, there are separate implementations for separate operating systems. This means that if the logic to extract the extension of a file differs on Mac from that on Linux, this distinction will be handled by those things. I don’t know of any such distinction so there might be none.
Edit: @Brian comments that an example like
/directory.ext/filewould of course not work with a simple.split('.')call, and you would have to know both that directories can use extensions, as well as the fact that on some operating systems, forward slash is a valid directory separator.This just emphasizes the use a library routine unless you have a good reason not to part of my answer.
Thanks @Brian.
Additionally, where a file doesn’t have an extension, you would have to build in logic to handle that case. And what if the thing you try to split is a directory name ending with a backslash? No filename nor an extension.
The rule should be that unless you have a specific reason not to use a library function that does what you want, use it. This will avoid you having to maintain and bugfix code others have perfectly good solutions to.