Get filename from Path in Python

Get filename from path in Python

There are different ways to define the path of the file. In Windows, the path separator for a file can be either a backslash or a forward slash. The ntpath module of Python will work on all platforms for all paths. The forwardslash / is used for UNIX and MacOS operating system and backslash \ for windows os.

This article will introduce some methods to get the filename from path in python. It also includes some sample code to help illustrate the ideas associated with different operating systems.

Get filename from Path in Python using the ntpath Module

Windows

On Windows systems, the ntpath module offers os.path capability. It may also be used on other platforms to handle Windows paths. The basename() function is supported by the ntpath library. ntpath.basename(path) is a function that is supplied a path and returns the filename from that path after execution.

The code example is given below.

The output is:

demo.txt

Linux

This module is Linux compatible. Filenames on Linux, on the other hand, may contain backslashes. So, for Linux, r’Users/Public\test’ always refers to the User folder’s Public\test file.

The code example is given below.

The output is:

demo.txt

When both back and forward slashes are used in a path, we should know which platform we are using. Otherwise, we might not be able to appropriately understand the path.

Get filename from Path in Python using the os.path.basename() Function

In this method, we use a function basename() that is provided by the os.path library. The basename() function uses the file path as a parameter and it will return the name of the file.

The code example is given below.

The output is:

demo.txt

This function works with any path format supported by the operating system.

Get filename from Path in Python using the os.path.split() Function

If we want to extract the head and tail of the file path, we can use this function. The file path is taken as an argument and the function split the head and tail of it.

The code example is given below.

The output is:

Users\Public\test
demo.txt

Get filename from Path in Python using the pathlib.path().name Method

The Pathlib module offers classes with semantics suited for multiple operating systems to represent file system paths. The Path() function returns the whole filepath, which we can then use with the name field to get the filename.

The code example is given below.

The output is:

demo.txt

Conclusion

os.path.basename or os.path.split won’t work in all the cases. If you are running a script on Linux and try to get windows path, it will fail. You can use npath.basename() which will work in all the scenarios.

That’s all about how to get filename from Path in Python

Related Posts

  • Iterate through files in directory in Python
    30 August

    Iterate through files in directory in python

    Table of ContentsUsing the os.scandir() function to iterate through files in a given directory in Python.Using the os.listdir() function to iterate through files in a given directory in Python.Using the os.walk() function to iterate through files in a given directory in Python.Using the pathlib module to iterate through files in a given directory in Python.Using […]

  • Write a list to a file in Python
    12 June

    Write a list to a file in python

    Table of ContentsUsing the write() function to write a list to a file in PythonUsing the writelines() function to write a list to a file in PythonUsing the pickle.dump() function to write a list to a file in PythonUsing the json.dump() function to write a list to a file in PythonUsing the unpack operator * […]

  • Python read text file line by line
    13 May

    Read text file line by line in Python

    Learn about how to read text file line by line in Python.

  • 26 March

    Python list files in directory

    Table of ContentsUsing os.walkUsing os.listdir(path)Using globConclusion In this post, we will see how to list all files in a directory in Python. There are multiple ways to list all files in a directory in Python. Using os.walk Python os module provides multiple function to get list of files. List all files in directory and its […]

  • 23 March

    Python write to text file

    Table of ContentsCreate txt file(‘x’)Write to txt file(‘w’)Append to txt file(‘a’) In this post, we will see how to write to a text file. There are multiple modes in which you can write to text file. Create txt file(‘x’) It will open the file in 'X' mode which will create new file and return error […]

  • 23 March

    Python read file into string

    Table of ContentsUsing read() methodUsing pathlib In this post, we will see how to read a file into a string variable. There are multiple ways to read file into String in Python. Using read() method Here are the steps to read file into string in python. Open the file in read mode using open() method […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.