How to get the filename without the extension in Python 

How to Get the filename without the extension in Python. That’s the topic I want to introduce to you today. There are many ways to get the filename without the extension in Python, but I will only introduce the three most optimal ways: using functions from the os module and the pathlib module. For details on the functions, please read the article below.

Get the filename without the extension in Python.

Information is kept in files that may be read from and written to. In Python, the following operations can be done on files: read, write, open, close, rename, and delete. We can save the information inside the computer’s storage with the aid of files. Every file has a pathname that identifies its storage location. We must first distinguish between the pathname and the extension. The filename and directory path will then be separated from the pathname.

Use os.path.splitext() method

Syntax:

os.path.splitext(path)

The os.path.splitext() method is used to get a filename without extension. This method will split the whole path into extension and root.

Example:

  • Import the os module.
  • The variable ‘dir’ contains the full path of the file.
  • The variable ‘rootName’ stores the pathname, and the variable ‘extension’ stores the file’s extension.
  • Separating the variable ‘runtIme’ by the delimiter produces a list of elements.
  • Index [-1] means getting the list’s first element from the right, which is the filename.
import os

# The full path of the file
directory = '/Users/admin/Python/data.txt'

# Create two variables that store the extension and the root
rootName, extension = os.path.splitext(directory)
print(rootName, extension)

# Perform split and get the filename
fileName = rootName.split('/')
print(fileName)
print('Filename:', fileName[-1])

Output:

/Users/admin/Python/data .txt
['', 'Users', 'admin', 'Python', 'data']
Filename: data

Use the os.path.basename() method

The continuation of a function from the os module that can be used to get the filename is the os.path.basename() method.

Syntax:

os.path.basename(path)

The os.path.basename() method gets the base name of the file from the full directory name.

Example:

  • The os.path.basename() method gets the base name of the file. 
import os

# The full path of the file
directory = '/Users/admin/Python/data.txt'

# The os.path.basename() method gets the base name of the file
# Perform split and get the filename
fileName = os.path.basename(directory).split('.')
print(fileName)
print('Filename:', fileName[0])

Output:

['data', 'txt']
Filename: data

Use the pathlib.Path.stem method. 

For handling file paths, Python uses the pathlib module. pathlib.Path.stem can be used to avoid obtaining the complete path. We may obtain the filename without the extension by using the root attribute.

Example:

import pathlib

# The full path of the file
directory = '/Users/admin/Python/data.txt'

# Use the pathlib.Path.stem method to get the filename without the extension
fileName = pathlib.Path(directory).stem
print('Filename:', fileName)

Output:

Filename: data

Summary:

The article about getting the filename without the extension in Python would like to stop here. I hope you found a method that works for you if you need ideas. You can choose one of three methods because either is optimal for this topic. If you have any suggestions or comments about the article, please comment.

Leave a Reply

Your email address will not be published. Required fields are marked *