How To Get The Filename Without The Extension From A Path In Python?

Rate this post

Problem Statement: How to get the filename without the extension from a path in Python?

Example: Suppose you have a file with the following path: C:\Users\SHUBHAM SAYON\Documents\folder1

Here, we just need to get the filename, i.e. “demo“. 

In Python, files are used to store information.  We can perform many operations on the files- read, write, open and close. Every file has a pathname that tells where the file gets stored. The pathname consists of information like the name of the file and the extension with which the file is stored. Sometimes, we just need to get the file name out of the whole pathname. Let’s look at the different methods to get the filename without the extension from a path in Python.

📹Video Walkthrough

Method 1: Using os.path.splitext()

We can use the os module in Python to get the filename without the extension from a path. Here, we have to import the os module first and further use the os.path.splitext() method for getting the file name. The os.path.splitext(path) method splits the path into root and extension pair such that root + extension == path

Syntax: os.path.splitext(path)

The method takes a pathname as the argument and returns a tuple that contains the separated extension and root names.

Example:

# Importing the os module
import os

# Full path
path = r"C:\Users\SHUBHAM SAYON\Documents\folder1\demo.txt"

pathname, extension = os.path.splitext(path)
file = pathname.split('\\')

# Printing the filename without extension
print(file[-1])

# OUTPUT --> demo

Method 2: Using The split() Method

The split() function is similar to the splitext() method that can be used to get a filename without an extension from the path. We don’t need to import any special module for using the split() function, we only have to call the function twice. The first split() function will split the whole pathname with the extension. The separator for this function will be ‘.’ and we will store the output into a variable. Further, we will use the second split() function on this variable by using the separator forward-slash ‘/‘. This will separate the path. Further, we just need to print the last element (filename) from this path.

Example:

# Full path
path = r"C:\Users\SHUBHAM SAYON\Documents\folder1\demo.txt"
# First split using separator '.'
name = path.split('.')
# Second split using separator '/'
file = name[0].split('\\')
# Printing the filename without extension
print(file[-1])

# OUTPUT --> demo

Note: We can print the complete pathname, by simply printing ‘name[0]‘.

Method 3: Using The rfind() Method

We can use the rfind() method to get the filename without the extension from a path in Python. The function separates the pathname and the extension and is used to find the last occurrence of the given value. 

Syntax: .rfind(item, start, end)

Here, the parameter “item” returns the last occurrence of the given value. The start and end arguments represent the starting and ending positions, respectively, while searching the string. The start value is 0 by default, and the end value is the total length of the string.

We have to call the rfind() method using the path.rfind() and we must pass the ‘.‘ as the value inside that function. We can save this into the variable and then print the pathname from the start to the end. To print just the filename, we have to use the split function after the rfind() method. We will print the last element from the whole path.

Example:

# Full path
path = r"C:\Users\SHUBHAM SAYON\Documents\folder1\demo.txt"
v = path.rfind(".")
# Printing the whole path without extension
x = path[:v]
print(x)
file = x.split('\\')
# Printing the filename without extension
print(file[-1])

Output:

C:\Users\SHUBHAM SAYON\Documents\folder1\demo
demo

Method 4: Using pathlib.Path.stem() From The pathlib Module

We have to import the pathlib module to use the pathlib.Path.stem property in Python to get the filename without the extension from a path. The Path() method takes the whole path as an input and extracts the file name from the whole path and returns the file name with the help of the stem method. The stem property directly returns the filename without the extension. 

Syntax: pathlib.Path(path).stem

Example:

# Importing the pathlib module
import pathlib

# Full path
path = r"C:\Users\SHUBHAM SAYON\Documents\folder1\demo.txt"

# Using the stem method from the module
file = pathlib.Path(path).stem
# Printing the filename without extension
print(file)

Method 5: Using The rpartition() Function

The rpartition() function can be used to get the filename without the extension from a file path in Python. The function searches for the last occurrence of the string and splits it into a tuple containing three elements. Among those three elements, the first one is the separator used. The second and third element is the string before the separator and the string after the separator.

Here, after separating, the first path will include the whole path without the extension. To print just the file name without the extension, we have to use the split function after the rpartition() function. We will print the last element from the whole path.

Syntax: .rpartition(value)

Example:

# Full path
path = r"C:\Users\SHUBHAM SAYON\Documents\folder1\demo.txt"
file = path.rpartition('.')
# Printing the whole path without extension
p = file[0]
print(p)
file_n = p.split('\\')
# Printing the file name without extension
print(file_n[-1])

Output:

C:\Users\SHUBHAM SAYON\Documents\folder1\demo
demo

Method 6: Using Basename() function

We can use another method – basename() from the os module to get the filename without the extension from a path. The path.basename() method in Python takes the file path as input and is used to return the basename. The method extracts the base name from the file path, and further, we will have to use the split.text() function to return the file name without the extension.

Example:

# Importing the os module
import os
# Full path
path = r"C:\Users\SHUBHAM SAYON\Documents\folder1\demo.txt"
file = os.path.basename(path)
# Printing the file name with extension
print(file)
file_n = os.path.splitext(file)[0]
# Printing the file name without extension
print(file_n)

Output:

demo.txt
demo

Conclusion

We learned various methods and used different modules (os and pathlib module) to get the filename without the extension from a path in Python in this tutorial. I hope you found this article helpful. Please stay tuned and subscribe for more such articles. Happy learning!

Related Tutorials:

Authors: Shubham Sayon and Rashi Agarwal