Skip to Content

Python – Get Filename from Path with Examples

In this tutorial, we will look at how to get the filename from a path using Python.

There are a number of ways to get the filename from its path in python. You can use the os module’s os.path.basename() function or os.path.split() function. You can also use the pathlib module to get the file name.

Let look at the above-mentioned methods with the help of examples. We will be trying to get the filename of a locally saved CSV file in python.

The os module comes with a number of useful functions for interacting with the file system. You can use the following functions from the os to get the filename.

The os.path.basename() function gives the base filename from the passed path. For example, let’s use it to get the file name of a CSV file stored locally.

import os

# the absoulte path of the file
file_path = r"C:\Users\piyush\Documents\Projects\movie_reviews_data\IMDB Dataset 5k.csv"
# get the filename
print(os.path.basename(file_path))

Output:

IMDB Dataset 5k.csv

You can see that we get the filename along with its extension as a string. To get the filename without its extension you can simply split the text on “.” and take the first part.

# filename without extension
print(os.path.basename(file_path).split(".")[0])

Output:

IMDB Dataset 5k

You might also be interested in knowing how to Get File size using Python

You can also use the os.path.split() function to get the filename. It is used to split the pathname into two parts – head and tail, where the tail is the last pathname component and the head is everything leading up to that. For example, for the path a/b/c.txt, the tail would be c.txt and the head would be a/b

Let’s now use it to find the filename of the CSV file from its path.

# the absoulte path of the file
file_path = r"C:\Users\piyush\Documents\Projects\movie_reviews_data\IMDB Dataset 5k.csv"
# split the file path
head, tail = os.path.split(file_path)
# get the filename
print(tail)

Output:

IMDB Dataset 5k.csv

You can see that the tail gives us the filename. Let’s now go ahead and see what do we have in the head part.

# show the path head
print(head)

Output:

C:\Users\piyush\Documents\Projects\movie_reviews_data

The head contains the part of the file path leading up to the filename. Note that the os.path.split() function determines the head and tail based on the occurrence of the last directory separator \ or / depending on the OS. For example, if the path ends in a separator it will give an empty string as the tail.

For more on the os.path.split() function, refer to its documentation.

For python versions 3.4 and above, you can also use the pathlib module to interact with the file system in python. Among other things, you can use it to get the filename from a path. For example, let’s get the filename of the same CSV file used above.

from pathlib import Path

# the absoulte path of the file
file_path = r"C:\Users\piyush\Documents\Projects\movie_reviews_data\IMDB Dataset 5k.csv"
# get the filename
print(Path(file_path).name)

Output:

IMDB Dataset 5k.csv

You can see that we get the correct filename using the name attribute of Path.

With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel.


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Tutorials on interacting with the file system in Python –

Author

  • Piyush

    Piyush is a data scientist passionate about using data to understand things better and make informed decisions. In the past, he's worked as a Data Scientist for ZS and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.