
Austin M. answered 01/11/21
Recent Master's Graduate with a Passion for Helping You Learn
There are multiple ways of determining the filename without an extension in Python. Some are more portable than others.
Lets consider the basic approach using no libraries, given the path is "/home/user/code/program.out".
We already know that ".out" is the extension and that "program" is the filename. We also know they are separated by a "." character. We can then see that no dots are used elsewhere. Furthermore, directories are separated by '/' characters and the filename is the last in the path list. So lets try the following solution.
This works for this example. However, it fails in the following two cases. What if the filename contains multiple '.' characters? Or, what if there is no file extension? For example, "/code/program.a.out". What if the path is separated by '\' instead of '/'? This happens with Windows vs. Linux. So it appears that our solution needs tweaking. We could further refine the solution to handle naming issues like the multiple '.' issue. We could also refine the solution to handle platform specific issues such as the Windows vs. Linux separator issue. However, Python has libraries built-in to handle this for us.
Before we look at Python's libraries, let us summarize our previous algorithm for getting the filename. First, we need to get the filename. Then, we need to separate the filename from the extension. (The first and second step can be in any order).
Let us start with the older library "os.path". This library handles path manipulation in a platform independent manner. Looking through the "os.path" documentation, we can find two functions that seem to fit our need, "split and splitext". "split" splits the path into two parts, the path to a file and the file. If there is no file, then the path is output and the file is empty. For example, (path_to, filename) = ('/somepath', ''). "splitext" splits the extension off of a filename. Note, some operating systems have files that begin with a '.'. These are treated as files and not extensions (see https://docs.python.org/3/library/os.path.html - "os.path.splitext" documentation). For example, Linux uses this naming to denote hidden files.
With these two commands, we can now figure out how to get a filename and extension separated.
Handling of potential cases so as empty paths or empty filenames is left to further study. I would recommend pulling up a Python interpreter and toying around with some of these cases to see how the "split" and "splitext" functions handle them.
There is one more library to handle path manipulations; however, it is only in newer versions of Python. It is left to further study. See https://docs.python.org/3/library/pathlib.html.
For documentation on "os.path", see https://docs.python.org/3/library/os.path.html.
Austin M.
ntpath is for Windows paths. While this happens to work for your example, it is more generic to use "os.path" as per the Python documentation at https://docs.python.org/3/library/os.path.html01/11/21