Python - How To Get Directory From Path in Python

ID : 522

viewed : 135

Tags : PythonPython DirectoryPython Path

vote vote

93

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.sep or os.path.sep. It will return the path separator used by your system.

Use 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 os.path.basename() function.

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) 

Output:

Use os.path.basename to Find Filename from the File Path in Python

Use 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)[0] 

Output:

Use os.path.splittext to Find Filename from the File Path in Python

Use os.path.dirname to Find Directory Name From the File Path in Python

The function 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) 

Output:

Use os.path.dirname to Find Directory Name from the File Path in Python

This function returns the complete path to the parent directory.

Use the pathlib Module to Extract Directory Name in Python

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:

str(p.parent) 

Output:

Use the pathlib Module to Extract Directory Name in Python

To fetch the directory and filename as parts, use the part function of the path module. For example:

P.parts 

You will get something like this.

Use the pathlib Module to Extract Directory Name in Python

Use 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)  

Output:

Use os.path.abspath to Find Directory Name from the File Path in Python

Here is the code to get the absolute path:

directory = os.path.abspath('.')  print(directory)  

Output:

'C:\Users\User\Desktop' 

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 OS and Path modules.

  • Related HOW TO?