Get filename from path without extension using Python
Last Updated :
11 Apr, 2025
Getting a filename from Python using a path is a complex process. i.e, In Linux or Mac OS, use "/" as separators of the directory while Windows uses "\" for separating the directories inside the path. So to avoid these problems we will use some built-in package in Python.
File Structure: Understanding Paths
path name root ext
/home/User/Desktop/file.txt /home/User/Desktop/file .txt
/home/User/Desktop /home/User/Desktop {empty}
file.py file .py
.txt .txt {empty}
Here, ext stands for extension and has the extension portion of the specified path while the root is everything except ext part. ext is empty if the specified path does not have any extension. If the specified path has a leading period (‘.’), it will be ignored.
Get filename without extension in Python
Get the filename from the path without extension split()
Python's split() function breaks the given text into a list of strings using the defined separator and returns a list of strings that have been divided by the provided separator.
Python
import os
path = 'D:\home\Riot Games\VALORANT\live\VALORANT.exe'
print(os.path.basename(path).split('.')[0])
Output:
VALORANT
Get filename from the path without extension using Path.stem
The Python Pathlib package offers a number of classes that describe file system paths with semantics suitable for many operating systems. The standard utility modules for Python include this module. Although stem is one of the utility attributes that makes it possible to retrieve the filename from a link without an extension.
Python
import pathlib
path = 'D:\home\Riot Games\VALORANT\live\VALORANT.exe'
name = pathlib.Path(path).stem
print(name)
Output:
VALORANT
Get the filename from the path without extension using rfind()
Firstly we would use the ntpath module. Secondly, we would extract the base name of the file from the path and append it to a separate array. The code for the same goes like this. Then we would take the array generated and find the last occurrence of the "." character in the string. Remember finding only the instance of "." instead of the last occurrence may create problems if the name of the file itself contains ".". We would find that index using rfindand then finally slice the part of the string before the index to get the filename. The code looks something like this. Again you can store those filenames in a list and use them elsewhere but here we decided to print them to the screen simply.
Python
# import module
import ntpath
# used path style of both the UNIX
# and Windows os to show it works on both.
paths = [
"E:\Programming Source Codes\Python\sample.py",
"D:\home\Riot Games\VALORANT\live\VALORANT.exe"]
# empty array to store file basenames
filenames = []
for path in paths:
# used basename method to get the filename
filenames.append(ntpath.basename(path))
# get names from the list
for name in filenames:
# finding the index where
# the last "." occurs
k = name.rfind(".")
# printing the filename
print(name[:k])
Output:

Get filename with entire path without extension
Get filename from the path without extension using rpartition()
Similar to how str.partition() and str.split operate, rpartition(). It only splits a string once, and that too in the opposite direction, as opposed to splitting it every time from the left side (From the right side).
Python
path = 'D:\home\Riot Games\VALORANT\live\VALORANT.exe'
print(path.rpartition('.')[0])
Output:
D:\home\Riot Games\VALORANT\live\VALORANT
Get filename from the path without extension using splitext()
The os.path.splitext() method in Python is used to split the path name into a pair root and ext.
Python
import os
path = 'D:\home\Riot Games\VALORANT\live\VALORANT.exe'
print(os.path.splitext(path)[0])
Output:
D:\home\Riot Games\VALORANT\live\VALORANT
Get filename from the path without extension using rsplit()
Python String rsplit() method returns a list of strings after breaking the given string from the right side by the specified separator.
Python
path = 'D:\home\Riot Games\VALORANT\live\VALORANT.exe'
print(path.rsplit('.', 1)[0])
Output:
D:\home\Riot Games\VALORANT\live\VALORANT
Similar Reads
How to get filename without extension in Ruby?
In this article, we will learn how to get a filename in Ruby without its extension. We can use the File.basename method to extract the last name of a filename. In Ruby, the File.basename method returns the last component of the filename. Syntax: File.basename(file_path [,suffix]) If we don't provide
1 min read
How to get file extension in Python?
In this article, we will cover How to extract file extensions using Python. How to Get File Extension in Python? Get File Extension in Python we can use either of the two different approaches discussed below: Use the os.path Module to Extract Extension From File in PythonUse the pathlib Module to Ex
2 min read
Python | Passing Filenames to Extension in C
Filename has to be encoded according to the systemâs expected filename encoding before passing filenames to C library functions. Code #1 : To write an extension function that receives a filename static PyObject* py_get_filename(PyObject* self, PyObject* args) { PyObject* bytes; char* filename; Py_ss
2 min read
How to get a File Extension in PHP ?
In this article, we will learn how to get the current file extensions in PHP. Input : c:/xampp/htdocs/project/home Output : "" Input : c:/xampp/htdocs/project/index.php Output : ".php" Input : c:/xampp/htdocs/project/style.min.css Output : ".css" Using $_SERVER[âSCRIPT_NAMEâ]: $_SERVER is an array o
2 min read
How to get file extensions using JavaScript?
JavaScript provides several methods to extract the file extension from a file name. Below are the three commonly used methods: Letâs see each of these methods one by one with examples. Table of Content Using split() and pop() MethodUsing substring() and lastIndexOf() MethodUsing match() Method with
3 min read
How to save file with file name from user using Python?
Prerequisites: File Handling in PythonReading and Writing to text files in Python Saving a file with the user's custom name can be achieved using python file handling concepts. Python provides inbuilt functions for working with files. The file can be saved with the user preferred name by creating a
5 min read
Python | os.path.supports_unicode_filenames object
OS module in Python provides functions for interacting with the operating system. OS comes under Pythonâs standard utility modules. This module provides a portable way of using operating system dependent functionality. os.path module is sub module of OS module in Python used for common path name man
1 min read
Python - List files in directory with extension
In this article, we will discuss different use cases where we want to list the files with their extensions present in a directory using python. Modules Usedos: The OS module in Python provides functions for interacting with the operating system.glob: In Python, the glob module is used to retrieve fi
3 min read
Python PIL save file with datetime as name
In this article, we are going to see how to save image files with datetime as a name using PIL Python. Modules required: PIL: This library provides extensive file format support, an efficient internal representation, and fairly powerful image processing capabilities. pip install Pillow datetime: Thi
2 min read
Python - Loop through files of certain extensions
A directory is capable of storing multiple files and python can support a mechanism to loop over them. In this article, we will see different methods to iterate over certain files in a given directory or subdirectory. Path containing different files: This will be used for all methods. Method 1: Usin
4 min read