Friday, December 27, 2024
Google search engine
HomeLanguagesGet filename from path without extension using Python

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.

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.

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

Python3




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. 

Python3




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.

Python3




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


Output:

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

RELATED ARTICLES

Most Popular

Recent Comments