Skip to content

Python: Get Filename From Path (Windows, Mac & Linux)

Python Get Filename from Path Cover Image

In this tutorial, you’ll learn how to use Python to get a filename from a path for a given file. You’ll learn how to do this using Windows, Mac, and Linux. Paths consist of three main parts: the directory, the filename, and the extension. The directory tells us where the file is, the filename tells us what the file is called, and the extension is the file type. Knowing how to get a filename can be an important skill when you’re trying to process different files.

You’ll learn how to get the filename form a Python path using the os library, the string .split() method, and the popular pathlib library.

The Quick Answer: Use os splittext() or pathlib

Quick Answer Python Get Filename from Path

How Are Paths Different in Windows and Mac/Linux?

Paths in Windows are different than they are in Mac and Linux operating systems. A key difference is the path separator the operating systems use. A path separator separates the directories from one another and allows us to identify the path of a file.

A Windows based operating system uses the backslash \ to separate paths. Meanwhile, Linux and Mac operating systems use the slash / to separate paths.

The problem with this is that the backslash is actually the escape character. Because of this, it can be helpful when working with paths, such as with string methods, to turn the string that contains our path into a raw string. This can be done by prepending the letter r to the front of a string.

Want to learn more about Python list comprehensions? Check out this in-depth tutorial that covers off everything you need to know, with hands-on examples. More of a visual learner, check out my YouTube tutorial here.

Use the os splittext Method in Python to Get the Filename from a Path

The built-in os library comes with a helpful module, the pathname module, that allows us to work with system paths. The pathname module comes with a helpful function, the basename() function. This returns the base name of the file, meaning that it also returns the extension of the file.

Let’s take a look at what this looks like:

# Get filename from path using os
import os

path = "/Users/datagy/Desktop/SomeImportantFile.py"
filename = os.path.basename(path)

print(filename)

# Returns: SomeImportantFile.py

We can see here that the script has returned the filename as well as the extension of the file.

Get the Filename from a Path without the Extension

If we wanted to remove the extension, we could write the following:

# Get filename from path using os
import os

path = "/Users/datagy/Desktop/SomeImportantFile.py"
filename_with_extension = os.path.basename(path)
filename = filename_with_extension.split('.', 1)[0]

print(filename)

# Returns: SomeImportantFile

This works because when we split the text and grab the first item, we only include the filename, rather than the extension as well. We pass in the second argument into the .split() function as the maxsplit= argument to tell Python how often to split the text.

In the next section, you’ll learn how to use the string .split() method to get the filename from a path in Python.

Want to learn how to get a file’s extension in Python? This tutorial will teach you how to use the os and pathlib libraries to do just that!

Use Python split to Get the Filename from a Path

We can get a filename from a path using only string methods, in particular the str.split() method. This method will vary a bit depending on the operating system you use.

In the example below, we’ll work with a Mac path and get the filename:

# Get filename from path using string methods
path = "/Users/datagy/Desktop/SomeImportantFile.py"
filename = path.split('/')[-1]

print(filename)

# Returns: SomeImportantFile.py

This returns the filename with the extension. If we wanted to return only the filename without the extension, we can simply split the filename again, this time using the . character.

Let’s take a look at what this would look like:

# Get filename from path using string methods
path = "/Users/datagy/Desktop/SomeImportantFile.py"
filename_with_extension = path.split('/')[-1]
filename = filename_with_extension.split('.')[0]

print(filename)

# Returns: SomeImportantFile

In the next section, you’ll learn how to use the object-oriented pathlib library to get the filename form a Python path.

Need to automate renaming files? Check out this in-depth guide on using pathlib to rename files. More of a visual learner, the entire tutorial is also available as a video in the post!

Use Python Pathlib to Get the Filename from a Path

The pathlib library uses a bit of a different approach to handling paths than the other methods we’ve covered so far. In fact, it uses an object-oriented approach to handle file paths. This is great, as it means that once we generate a path object, we can easily access different attributes about it.

One of these attributes is the .stem attribute, that provides the filename of a provided path object.

Let’s take a look at how we can accomplish this in Python:

# Get filename from path using pathlib
import pathlib
path = "/Users/datagy/Desktop/SomeImportantFile.py"

path_object = pathlib.Path(path)
filename = path_object.stem

print(filename)

# Returns: SomeImportantFile

This approach is a bit different and for users not familiar with object-oriented design, it may seem a bit strange. But it is a very easy and intuitive approach, once you get the hang of it.

Check out some other Python tutorials on datagy, including our complete guide to styling Pandas and our comprehensive overview of Pivot Tables in Pandas!

Conclusion

In this post, you learned how to use Python to get the filename from a path, in Windows, Mac, and Linux. You learned how to use the helpful os library to accomplish this, as well as some trickier string methods. You also learned how to use the more modern, object-oriented pathlib library to get the filename from a path.

To learn more about the pathlib library, check out the official documentation here.

Nik Piepenbreier

Nik is the author of datagy.io and has over a decade of experience working with data analytics, data science, and Python. He specializes in teaching developers how to use Python for data science using hands-on tutorials.View Author posts

Tags:

Leave a Reply

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