Table of Contents
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.
1 2 3 4 5 6 7 8 9 10 |
import os filepath = '/home/aditya1117/PycharmProjects/pythonProject/Demo.csv' print("The file path is:") print(filepath) directoryName = os.path.dirname(filepath) print("The directory is:") print(directoryName) |
Output:
1 2 3 4 5 6 |
The file path is: /home/aditya1117/PycharmProjects/pythonProject/Demo.csv The directory is: /home/aditya1117/PycharmProjects/pythonProject |
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.
1 2 3 4 5 6 7 8 9 10 |
import os filepath = '/home/aditya1117/PycharmProjects/pythonProject' print("The file path is:") print(filepath) directoryName = os.path.dirname(filepath) print("The directory is:") print(directoryName) |
Output:
1 2 3 4 5 6 |
The file path is: /home/aditya1117/PycharmProjects/pythonProject The directory is: /home/aditya1117/PycharmProjects |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import os filepath = '/home/aditya1117/PycharmProjects/pythonProject/Demo.csv' print("The file path is:") print(filepath) nameTuple = os.path.split(filepath) print("The tuple is:") print(nameTuple) directoryName = nameTuple[0] print("The directory is:") print(directoryName) |
Output:
1 2 3 4 5 6 7 8 |
The file path is: /home/aditya1117/PycharmProjects/pythonProject/Demo.csv The tuple is: ('/home/aditya1117/PycharmProjects/pythonProject', 'Demo.csv') The directory is: /home/aditya1117/PycharmProjects/pythonProject |
If we pass the file path of a directory to the split()
function, we can get the name of the parent directory as follows.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import os filepath = '/home/aditya1117/PycharmProjects/pythonProject' print("The file path is:") print(filepath) nameTuple = os.path.split(filepath) print("The tuple is:") print(nameTuple) directoryName = nameTuple[0] print("The directory is:") print(directoryName) |
Output:
1 2 3 4 5 6 7 8 |
The file path is: /home/aditya1117/PycharmProjects/pythonProject The tuple is: ('/home/aditya1117/PycharmProjects', 'pythonProject') The directory is: /home/aditya1117/PycharmProjects |
Further reading:
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.
1 2 3 4 5 6 7 8 9 10 11 |
import pathlib filepath = '/home/aditya1117/PycharmProjects/pythonProject/Demo.csv' print("The file path is:") print(filepath) pathObject = pathlib.Path(filepath) directoryName = pathObject.parent print("The directory is:") print(directoryName) |
Output:
1 2 3 4 5 6 |
The file path is: /home/aditya1117/PycharmProjects/pythonProject/Demo.csv The directory is: /home/aditya1117/PycharmProjects/pythonProject |
Similarly, we can get the parent directory of a directory using the pathlib
module as follows.
1 2 3 4 5 6 7 8 9 10 11 |
import pathlib filepath = '/home/aditya1117/PycharmProjects/pythonProject' print("The file path is:") print(filepath) pathObject = pathlib.Path(filepath) directoryName = pathObject.parent print("The directory is:") print(directoryName) |
Output:
1 2 3 4 5 6 |
The file path is: /home/aditya1117/PycharmProjects/pythonProject The directory is: /home/aditya1117/PycharmProjects |
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.
1 2 3 4 5 6 7 8 9 10 |
import os filepath = '/home/aditya1117/PycharmProjects/pythonProject/Demo.csv' print("The file path is:") print(filepath) filepath = os.path.dirname(filepath) print("The new file path is:") print(filepath) |
Output:
1 2 3 4 5 6 |
The file path is: /home/aditya1117/PycharmProjects/pythonProject/Demo.csv The new file path is: /home/aditya1117/PycharmProjects/pythonProject |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import os filepath = '/home/aditya1117/PycharmProjects/pythonProject/Demo.csv' print("The file path is:") print(filepath) nameTuple = os.path.split(filepath) print("The tuple is:") print(nameTuple) filepath = nameTuple[0] print("The new file path is:") print(filepath) |
Output:
1 2 3 4 5 6 7 8 |
The file path is: /home/aditya1117/PycharmProjects/pythonProject/Demo.csv The tuple is: ('/home/aditya1117/PycharmProjects/pythonProject', 'Demo.csv') The new file path is: /home/aditya1117/PycharmProjects/pythonProject |
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.
1 2 3 4 5 6 7 8 9 10 11 |
import pathlib filepath = '/home/aditya1117/PycharmProjects/pythonProject/Demo.csv' print("The file path is:") print(filepath) pathObject = pathlib.Path(filepath) filepath = pathObject.parent print("The new file path is:") print(filepath) |
Output:
1 2 3 4 5 6 |
The file path is: /home/aditya1117/PycharmProjects/pythonProject/Demo.csv The new file path is: /home/aditya1117/PycharmProjects/pythonProject |
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!