Get Directory Name From File Path in Python

File operations are a fundamental part of python application development. In this article, we will discuss how we can get the directory name from the file path in python. Later, we will also discuss how we can remove filename from the file path in python.

How to Get directory name from file path in python?

Python provides us with the os module to perform file operations. Additionally, we can also use the pathlib module to get the directory from the file path in python. Let us discuss programs to get directory from file path using both the modules in python.

Get Directory Name From File Path Using the os Module in Python

The os.path module provides us with the dirname() method and the split() method with which we can get the directory from the file name in python. Let us discuss both methods one by one.

Get Directory Name From File Path Using the os.path.dirname() Function

The os.path.dirname() function takes a file path as its input argument and returns a string containing the directory. For instance, if you will provide the file path of a file, the dirname() function will return the directory containing the file as shown below.

Output:

Similarly, if we pass the file path of a directory, the dirname() function will return the path of the directory containing the current directory. You can observe this in the following example.

Output:

Get Directory Name From File Path Using the os.path.split() Function

We can also get the directory from the file path using the os.path.split() function. The split() function takes the file name as its input argument and returns a tuple containing two strings. The first string contains the file path of the parent directory of the current file or directory. The second element of the tuple contains the name of the current directory or file. 

If given the file path of a file, we can get the directory using the split() function from the first element of the tuple returned by the split() function as follows.

Output:

If we pass the file path of a directory to the split() function, we can get the name of the parent directory as follows.

Output:

Get Directory Name From File Path Using the pathlib Module

Instead of using the os module, we can also use the pathlib module to get the directory from the file path in python. For this, we can use the Path() function. The Path() function takes the file path as its input argument and returns a POSIX path object in UNIX or ntpath object in NTFS (windows). 

To get the directory from the path object, we can use the parent attribute of the path object. The parent attribute of the path object contains the file path of the current directory. We can convert the POSIX or ntpath object containing the directory path in the parent attribute to a simple directory path using the str() method. In this way, we can get the directory from the file path using the pathlib module as follows.

Output:

Similarly, we can get the parent directory of a directory using the pathlib module as follows.

Output:

Remove FilnName from File Path

There are multiple ways to remove Filename from file path. Let’s go through them.

Remove Filename From the File Path Using the OS Module in Python

To remove the filename from the file path, we can use the os module and the pathlib module. 

To remove the filename from a file path using the os module, we will first get the directory from the file path using the os.path.dirnname() function. Then, we will assign the directory to the file path. In this way, we can remove the filename from the file path as shown below.

Output:

In a similar manner, we can remove the filename from a file path using the os.path.split() function. Here, we will assign the first element of the tuple given as output by the split function i.e. the directory name to the variable containing the file path. In this way, we can remove the filename from the file path using the os.path.split() function as shown below.

Output:

Remove Filename From File Path Using the Pathlib Module in Python

To remove the filename from the file path using the pathlib module, we will first get the name of the parent directory using the parent attribute of the file path object returned by the Path() object. as discussed in the previous section. After that, we will assign the directory to the variable containing the original file path. In this way, we can remove the filename from the file path using the pathlib module as follows.

Output:

Conclusion

In this article, we have discussed how to get the directory name from the file path in python using different approaches. We also saw how we can filename from the file path in python. All of the approaches are semantically similar and have almost the same time complexity. So, you can use any of the approaches to get the work done. 

I hope you enjoyed reading this article. Stay tuned for more informative articles. 

Happy Learning!

Was this post helpful?


import_contacts

You may also like:

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

    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 […]

  • Python create file if not exists
    06 November

    Create file if not exists in Python

    Table of ContentsCreating Files in PythonWays to create file if not exists in PythonUsing the open() functionUsing the pathlib.Path.touch() functionUsing the os.path.exists() functionUsing the try and except blockConclusionWas this post helpful? Creating Files in Python File handling is a handy concept in programming. We can write and read data to files. In Python, to create […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.