Skip to content
Related Articles

Related Articles

Get filename from path without extension using Python

View Discussion
Improve Article
Save Article
  • Last Updated : 13 Jul, 2022

Getting a filename from Python using a path is a complex process not because of the regex we need to use to separate the filename from the path but because of the different types of separators used in file paths in different operating systems. 

For example, UNIX-based systems like Linux or Mac Os use “/”  (Forward Slash) as separators of the directory while Windows uses “\” (Back Slash) for separating the directories inside the path. So to avoid all these problems we would use a built-in package in Python.

Method 1: 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

Method 2: 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

Method 3:  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).

Python3




path = 'D:\home\Riot Games\VALORANT\live\VALORANT.exe'
print(path.rpartition('.')[0])

Output:

D:\home\Riot Games\VALORANT\live\VALORANT

Method 4:  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:


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!