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

Was this post helpful?

Related Posts

  • 19 May

    Remove Extension From Filename in Python

    Table of ContentsHow to Remove Extension From Filename in Python?Remove Extension From Filename in Python Using the os ModuleRemove Extension From Filename in Python using the pathlib moduleRemove Multiple File Extensions Using pathlib Module in PythonConclusionWas this post helpful? While programming in python, we often have to deal with file names. In this article, we […]

  • 19 May

    Get Directory Name From File Path in Python

    Table of ContentsHow to Get directory name from file path in python?Get Directory Name From File Path Using the os Module in PythonGet Directory Name From File Path Using the pathlib ModuleRemove FilnName from File PathRemove Filename From the File Path Using the OS Module in PythonRemove Filename From File Path Using the Pathlib Module […]

  • 19 May

    Print Current Directory in Python

    Table of ContentsFiles and Directories in PythonWays to print the Current Directory in PythonUsing the os.getcwd() function to print the current directory in PythonUsing the os.path module to print the current directory in PythonUsing the os.path.abspath() function to print the current directory in PythonUsing the pathlib.Path.cwd() function to print the current directory in PythonUsing the […]

  • Python copy file to another directory
    17 January

    How to copy file to another directory in Python

    Table of ContentsWays to copy file to another directoy in PythonUsing file handlingUsing the shutil libraryUsing the pathlib libraryUsing the os moduleUsing the subprocess moduleConclusionWas this post helpful? In this article, we will see different ways to copy file to another directory in Python. We can read and write files in Python. We can also […]

  • Copy file Python
    12 January

    How to Copy File in Python?

    Table of ContentsCopy file using the shutil module in PythonCopy file using the copyfileobj() function in PythonCopy file using the copyfile() function in PythonCopy file using the copy() function in PythonCopy file using the copy2() function in PythonCopy file using the operating system command in PythonCopy file using the os module in PythonCopy file using […]

  • Python absolute path
    17 November

    Get Absolute Path in Python

    Table of ContentsWhat is an absolute path to a file? How to get absolute path in PythonUse abspath to get the absolute path to file use pathlib module to get the absolute path to file ConclusionWas this post helpful? We often use files to store the outputs of our programs in permanent storage. Generally, we […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.