Python Get Filename From A Path Without Extension 4 Ways

Are you looking to get the filename present in a Path without extension or with extension? This article will show you how you can get the filename from a path.

This can be easily achieved in 6 different ways in Python. You can use the OS library here and the method available in that library such as path, scandir, listdir, etc. Or you can use the Pathlib library that also provides the feature to fetch the filenames from a given path.

1. Using path and OS library of Python to Get the Filename from a Path

In the OS library of Python, a method named path returns the filenames present in the input path passed as an argument to the function.

But this returns the complete name of the filename and if you want the filenames without extension then you need to use the split function to strip down the extension from the returned filename. Let us see the usage of Path from the OS library in the below code example.

#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[0])

Output:

C:\Test2\Test File

As you can see in the above example, I was able to print the path of the file with a filename without extension. Now if you do not want the complete Path to be associated with the Filename then in that case you can use the below example.

2. Using PathLib and Path to Get Filename from a Path

Using Path from pathlib library of Python you can easily print out the filename from a given path. Since in this case, we will not be getting the complete Path of the file and we’ll only be provided with 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)

Output:

Test File

As you can see in the above code using the Path you can easily print the filename from the given Path all you are required to use is the stem method that removes the extension and the path from the given Path String.

3. Using basename and Path from OS library to get the Filename from a Path

Another way to get the filename from a Path is using the basename method and the Path from the OS library of Python.

Let us see in the below example the usage of basename and 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[0])

Output:

Test File.jpg
Test File

In the above example using the basename method, I was able to get the filename out of the given Path in String. And then using the splitext method I was able to split the filename into an extension and filename without extension. And finally, I print out the filename without extension.

4. How To Remove Mutliple Extension from a Filename from a Path

Now if you have a Path and a filename that has an extension of more than one, for example, filezip.tar.gz, in this case, one of the above-mentioned methods will directly work. You will need to modify the above code slightly to get the filename without any kind of extension in it.

Let us see in the below example the usage of with_suffix and Path from Pathlib library to get filename out of any number of extensions.

#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)

Output:

FileTest

As you can see in the above code I was able to print only the filename without any extension even though it contained multiple extensions to it. The with_suffix method removes the last extension and the path from the string and then the stem removes the remaining extension.

Python Get Filename From A Path Without Extension 4 Ways

Wrap Up

I hope you were able to get the answer related to the problem of how to get the filename from a path using Python. I have listed the 4 different methods that you can use and encounter in daily use.

Let me know if have any better solution to any of the methods listed above I will be happy to add it.

If you liked the above tutorial then please follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog.

Further Read:

  1. How To Concatenate String and Integer In Python
  2. Code Example: Convert a String to DateTime in Python
  3. How Do I Sort A Dictionary By Value In Python
  4. Code Example: How To Add Numbers In A List Python
  5. Python – How To Delete All Files In A Directory
  6. How To Remove Non-alphanumeric Characters From String In Python
  7. How To Read Image Name From Folder in Python
  8. How To Multiply Without Using * In Python
  9. How To Add or Append Values To a Set In Python
  10. 4 Ways To List All Subdirectories in a Directory – Python
  11. Dict To list – Convert A Dictionary to a List in Python

Leave a Reply

Your email address will not be published. Required fields are marked *