One day I was trying to obtain a filename from a given path but I wasn’t having any idea how to do it. So, I researched the internet, and forums and came up with the working code that allowed me to get the filename.
In Python, this is easily accomplished in four different ways. You can use the OS library and the methods available in that library, such as path, scandir, listdir, and so on, in this case.
You can also use the Pathlib library, which includes the ability to retrieve filenames from a given path.
1. Using path and OS library of Python to Get the Filename from a Path
A path method in Python’s OS library returns the filenames found in the input path passed as an argument to the function.
However, this returns the entire filename, and if you want the filenames without the extension, you must use the split function to remove the extension from the returned filename.
In the following code example, we will see how to use Path from the OS library.
#importing the OS Library import os #Initializing the Path from where you need #The Filename. pathToGetFileNames = "C:\Test2\\Test File.jpg" filenameWithPath = os.path.splitext(pathToGetFileNames) #Printing the Path Without Extension of File print(filenameWithPath)
As you can see in the preceding example, I was able to print the path of the file with a filename without an extension. If you do not want the full Path to be associated with the Filename, use the example below.
2. Using PathLib and Path to Get Filename from a Path
You can easily print out the filename from a given path using Path from Python’s pathlib library. Because we will not get the full path of the file, in this case, we will only get the filename.
Let us see in the below example code the usage of Pathlib and Path.
#importing Path from Pathlib from pathlib import Path #Initializing the Path from where you need #The Filename. pathToGetFileName = "C:\Test2\\Test File.jpg" filenameWithPath = Path(pathToGetFileName).stem #Printing the FileName Without Extension of File print(filenameWithPath)
As you can see in the above code, you can easily print the filename from the given Path by using the stem method, which removes the extension and the path from the given Path String.
3. Using basename and Path from the OS library to get the Filename from a Path
Another method for obtaining the filename from a Path is to use the basename method and the Path from Python’s OS library.
In the following example, we will use basename and the OS library to get the filename from a Path in Python.
#importing OS Library import os #Initializing the Path from where you need #The Filename. pathToGetFileName = "C:/Test2/Test File.jpg" filenameWithPath = os.path.basename(pathToGetFileName) #Printing Filename with Extension print(filenameWithPath) #Splitting the Extension using splitext filenameWithPath = os.path.splitext(filenameWithPath) #Printing Filename without Extension print(filenameWithPath)
In the preceding example, I was able to extract the filename from the given Path in String using the basename method.
I was then able to split the filename into an extension and a filename without an extension by using the
splitext() method. Finally, I print the filename without the extension.
4. How To Remove Multiple Extensions from a Filename from a Path
If you have a Path and a filename with more than one extension, such as filezip.tar.gz, one of the above-mentioned methods will not work directly. You’ll need to tweak the above code slightly to get a filename without an extension.
Let’s look at how to use with suffixes and Path from the Pathlib library to get a filename from any number of extensions in the example below.
#importing Path from Pathlib Library from os import path from pathlib import Path #Initializing the Path from where you need #The Filename. pathToGetFileName = "C:/Test2/FileTest.tar.gz" filenameWithPath = Path(pathToGetFileName).with_suffix('').stem #Printing Filename without Extension print(filenameWithPath)
As you can see in the code above, I was able to print only the filename without any extensions, despite the fact that it contained multiple extensions.
The suffix method removes the string’s last extension and path, and the stem method removes the remaining extension.
I hope you were able to find an answer to the question of how to get the filename from a path in Python. I’ve listed four different methods that you may encounter on a daily basis.
Please let me know if you have a better solution to any of the methods listed above, and I will gladly add it.
- How To Concatenate String and Integer In Python
- Code Example: Convert a String to DateTime in Python
- How Do I Sort A Dictionary By Value In Python
- Code Example: How To Add Numbers In A List Python
- Python – How To Delete All Files In A Directory
- How To Remove Non-alphanumeric Characters From String In Python
- How To Read Image Name From Folders in Python
- How To Multiply Without Using * In Python