Situations often arise when we have the full name of a file, but need to know its extension. Or to add the desired extension, when we do not know whether the user has entered it or not. Sometimes we have a relative path to a file and need to know the absolute path. This article is about the basic methods of working with file names.
Absolute path to a file
To know the absolute path to a file in Python, we need to use the os library. Plug it in with the import os command. The path class has an abspath method. Here is an example of how to use it.
import os p = os.path.abspath('file.txt ') print(p) C:python3file.txt
You can also use the standard pathlib library. This is a standard library and has been integrated since Python 3.4. Before that, you had to install it with the pip install pathlib command. It is intended to work with paths of the filesystem in different operating systems and is perfectly suited for this task.
import pathlib p = pathlib.Path('file.txt ') print(p) C:python3file.txt
To get the filename from the full path string, we use the basename method of the os module.
import os name = os.path.basename(r'C:python3file.txt ') print(name) file.txt
Here I put r before the line to suppress possible occurrence of service characters. For example, in this case, if you didn’t put r, f would be considered a page translation character.
Now let’s find out how to get a filename in Python without an extension. Let’s use the splittext method. This time, we will take a file with a double extension as an example to see how the standard functions will work in this case.
from os import path full_name = path.basename(r'C:python3file.tar.gz ') name = path.splitext(full_name) print(name) file.tar
You can see that the last gz archiver extension was discarded, while the uncompressed tar archive extension remains in the name. If we need only the name, we can discard all characters of the resulting string, which come after the first dot. The dot character is also discarded. Let’s add the following code to the previous example:
index = name.index('.') print(name[:index]) file
In Python, you can get a file extension in a similar way using the same splitext function. This function returns a tuple. The first element of the tuple is the name and the second is the extension. In this case, we need the second element. The index of the second element is one, since they all start from zero.
from os import path full_name = path.basename(r'C:python3file.tar.gz ') name = path.splitext(full_name) print(name) .gz
Similarly we can use the pathlib library. We use the suffix method.
from pathlib import Path print(Path(r'C:python3file.tar.gz ').suffix) .gz
But in our case we have two extensions. You can find them out with the function suffixes. This function returns a list whose entries are the extensions. Below you can see an example of how to get a list of extensions.
from pathlib import Path print(Path(r'C:python3file.tar.gz ').suffixes) ['.tar', '.gz ']