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.

Python3




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.

Python3




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 rfind and 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.

Python3




# 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 from path without extension using Python

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.

Similar Reads

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}...

Get filename without extension in Python

Get the filename from the path without extension split()...

Get filename with entire path without extension

...