File paths are unique strings that represent the location of a file in a system or a directory. Sometimes, you may have to retrieve or extract any part or chunk from the file path.
There are several ways you can extract parts from the file path in Python.
Differences in File Path in Various OS
We use the forward-slash
/ in the Linux directory structure (including MAC), while in Windows, we use the backward slash
\ as the separator.
To check which separator your system uses, use the
os.path.sep. It will return the path separator used by your system.
os.path.basename to Find Filename From the File Path in Python
The first and the easiest way to extract part of the file path in Python is to use the
This function returns the filename from the file path along with its extension. Plus, it works for all the Python versions.
import os fpath='c:\Project\input.txt' os.path.basename(fpath)
os.path.splittext to Find Filename From the File Path in Python
If you want to extract just the filename from the file path and not its extension, you will use the
os.path.splittext() function. This function will only return the filename.
Furthermore, you will add an index
0 with this function to get the desired output. The
splittext() function splits the file path into an array. Hence, index
0 represents the filename, and index
1 represents its extension.
import os fpath='c:\Project\input.txt' fname=os.path.splitext(fpath)
os.path.dirname to Find Directory Name From the File Path in Python
os.path.dirname() is used to extract the directory name from the path. This function will return the directory name as the string on the Python console.
import os fpath='c:\Project\input.txt' dirname = os.path.dirname(filepath) print(dirname)
This function returns the complete path to the parent directory.
Another way to get the directory from the file path is to use the
pathlib module. This is specifically available in Python versions 3.4+.
The function takes an argument, the file path, and can return various outputs depending on the item fetched. Let’s import the file along with its path first.
from pathlib import Path p = Path('C:\\Program Files\\Internet Explorer\\iexplore.exe')
To check the parent directories, execute the following code:
To fetch the directory and filename as parts, use the part function of the path module. For example:
You will get something like this.
os.path.abspath to Find Directory Name From the File Path in Python
The OS module also offers the functionality to extract a directory from the file path.
This function takes two different arguments: backslash and dot character. The backslash character returns the root directory, and the dot returns the current directory.
import os directory = os.path.abspath('\\') print(directory)
Here is the code to get the absolute path:
directory = os.path.abspath('.') print(directory)
This tutorial looked at several functions and modules to split the file path in Python. We have also learned how to extract a name or directory from the file path in Python using