How to Get the Directory of a File in Python?

In this tutorial, I will explain how to get the directory of a file in Python. As a software developer working on a project for a US-based company, I recently encountered a situation where I needed to determine the directory path of a specific file. Python provides built-in modules and functions that make this task simple. Let us explore different methods to achieve this task along with examples.

Get the Directory of a File in Python

Let us see how to get the directory of a file in Python.

Read How to Get the File Size in MB using Python?

Method 1. Use the os Module

Python’s built-in module os provides functions for interacting with the operating system. One of its useful functions is os.path.dirname() , which returns the directory path of a given file.

Here’s an example of how to use os.path.dirname() to get the directory of a file:

import os

file_path = "/Users/JohnDoe/Documents/project/data.csv"
directory = os.path.dirname(file_path)
print("Directory:", directory)

Output:

Directory: /Users/JohnDoe/Documents/project

You can see the output in the below screenshot.

Get the Directory of a File in Python

In this example, we have a file named data.csv located in the "/Users/JohnDoe/Documents/project" directory. By passing the file_path to os.path.dirname(), we obtain the directory path, which is "/Users/JohnDoe/Documents/project".

Check out How to Check If a File Exists and Create It If Not in Python?

Get the Current Working Directory

In some cases, you may need to get the current working directory instead of the directory of a specific file. Python provides the os.getcwd() function to retrieve the current working directory.

Here’s an example:

import os

current_directory = os.getcwd()
print("Current working directory:", current_directory)

Output:

Current working directory: C:\Users\Public\code

You can see the output in the below screenshot.

How to Get the Directory of a File in Python

This code will output the current working directory path, which could be something like "/Users/JohnDoe/PythonProjects".

Read Python file Does Not Exist Exception

Method 2. Use the pathlib Module

Python 3.4 introduced the pathlib module, which provides an object-oriented approach to working with file paths. It offers a more intuitive and expressive way to handle file paths than the os module.

To get the directory of a file using pathlib, you can use the parent attribute of a Path object:

from pathlib import Path

file_path = Path("/Users/JohnDoe/Documents/project/data.csv")
directory = file_path.parent
print("Directory:", directory)

Output:

Directory: \Users\JohnDoe\Documents\project

You can see the output in the below screenshot.

Get the Directory of a File in Python pathlib Module

In this example, we create a Path object representing the file path. The parent attribute of the Path object gives us the directory path, which is "/Users/JohnDoe/Documents/project".

To get the current working directory using pathlib, you can use the Path.cwd() method:

from pathlib import Path

current_directory = Path.cwd()
print("Current working directory:", current_directory)

This code will output the current working directory path using the pathlib module.

Check out Python File methods

Handle Relative Paths

Sometimes, you may encounter relative file paths instead of absolute paths. Python provides functions to handle relative paths and convert them to absolute paths.

Here’s an example using the os module:

import os

relative_path = "data/user_info.txt"
absolute_path = os.path.abspath(relative_path)
directory = os.path.dirname(absolute_path)
print("Directory:", directory)

In this example, we have a relative file path "data/user_info.txt". By using os.path.abspath(), we convert the relative path to an absolute path. Then, we can use os.path.dirname() to extract the directory path from the absolute path.

Read How to Write a Variable to a File in Python?

Conclusion

In this tutorial, I explained how to get the directory of a file in Python. I discussed some methods such as using the os module, getting the current working directory , and using the pathlib module. I also discussed how to handle relative paths.

You may like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.