Get the last part of a path in Python

avatar

Borislav Hadzhiev

Last updated: Sep 23, 2022

banner

Photo from Unsplash

Get the last part of a path in Python #

Use the pathlib.PurePath class to get the last part of a path, e.g. last_part = pathlib.PurePath(path).name. The name attribute on the object returns a string that represents the last part of the provided path.

main.py
import pathlib path = '/home/borislav/Desktop/last/' last_part = pathlib.PurePath(path).name print(last_part) # 👉️ 'last'

If you have to process Windows-style paths on a Linux machine, use the ntpath module instead.

main.py
import ntpath def get_last_path(path): head, tail = ntpath.split(path) return tail or ntpath.basename(head) last_path = get_last_path('C:\\Users\\Borislav\\Desktop\\example.txt') print(last_path) # 👉️ example.txt

The pathlib.PurePath class represents the system's path flavor.

Instantiating the class creates either a PurePosixPath or a PureWindowsPath.

The PurePath.name attribute returns a string that represents the final path component, excluding the drive and root, if any.

main.py
import pathlib # 👇️ images print(pathlib.Path('/home/borislav/Desktop/images').name) # 👇️ example.txt print(pathlib.Path('/home/borislav/Desktop/example.txt').name) # 👇️ Desktop print(pathlib.Path('/home/borislav/Desktop/').name)

In the unlikely scenario that you have to process Windows-style paths on a Linux machine, use the ntpath module.

main.py
import ntpath def get_last_path(path): head, tail = ntpath.split(path) return tail or ntpath.basename(head) last_path = get_last_path('C:\\Users\\Borislav\\Desktop\\example.txt') print(last_path) # 👉️ example.txt

Windows paths can use either a backlash or a forward slash as the path separator and the ntpath module handles both cases.

The ntpath.split() method returns a tuple (head, tail) where tail is everything after the final slash.

If the path doesn't end in a slash, we return tail, otherwise, we use the ntpath.basename() method to return the final component of the pathname.

Alternatively, you can use the os.path.basename() method.

Get the last part of a path using os.path.basename() #

To get the last part of a path:

  1. Use the os.path.normpath() method to strip any trailing slashes from the path.
  2. Use the os.path.basename() method to get the last part of the path.
main.py
import os path = '/home/borislav/Desktop/last/' last_path = os.path.basename(os.path.normpath(path)) print(last_path) # 👉️ last

The os.path.normpath method normalizes a path by removing double slashes and stripping trailing slashes.

The os.path.basename method returns the final component of the path.

If used with a path that ends with a forward slash, the os.path.basename() method returns an empty string (everything after the last slash).

This is why we had to use the os.path.normpath method to strip any trailing slashes before calling os.path.basename.

If you have to process Windows-style paths on a Linux machine, use the ntpath module instead.

main.py
import ntpath def get_last_path(path): head, tail = ntpath.split(path) return tail or ntpath.basename(head) last_path = get_last_path('C:\\Users\\Borislav\\Desktop\\example.txt') print(last_path) # 👉️ example.txt
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.