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?

Leave a Reply

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