Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Benefits of os.path.splitext over regular .split?

Tags:

python

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.

like image 937
Jenn D. Avatar asked Feb 12 '09 18:02

Jenn D.


People also ask

What is OS path Splitext ()?

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.

Why do we use OS path join?

Using os. path. join makes it obvious to other people reading your code that you are working with filepaths. People can quickly scan through the code and discover it's a filepath intrinsically.

What is the use of OS path dirname (__ FILE __) in this method?

path. dirname() method in Python is used to get the directory name from the specified path.

What does OS path Basename do?

path. basename() method in Python is used to get the base name in specified path.


2 Answers

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/file would 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.

like image 139
Lasse V. Karlsen Avatar answered Oct 08 '22 19:10

Lasse V. Karlsen


os.path.splitext will correctly handle the situation where the file has no extension and return an empty string. .split will return the name of the file.

like image 27
Andrew Grant Avatar answered Oct 08 '22 19:10

Andrew Grant