HOWTO · Python
How to Get Directory From Path in Python
This tutorial demonstrates how to extract a part of the file path in Python.
On this page
To get the parent directory from a file path, pass the path to os.path.dirname(). For an object-oriented alternative, use Path(file_path).parent from pathlib. These return the directory containing the file; they do not report the process’s current working directory.
The examples use forward-slash paths so their results are easy to read. Python’s path functions interpret paths according to the host operating system. Use os.path.join() or pathlib to build paths when components come from separate values, rather than inserting a separator by hand.
Understand Path Separators
A path separates directory names and a final component with a platform-specific separator. POSIX systems commonly use /; Windows paths commonly use \, and Windows also accepts / in many path operations. os.sep gives the separator used by the current platform. For ordinary path handling, pass a path to os.path or use pathlib instead of branching on the separator yourself.
Get a File Name with os.path.basename
os.path.basename() returns the final component of a path. For a file path, that is usually the filename, including its extension; it does not return the directory. To get the final directory name from a file path, first take its parent with os.path.dirname(), then pass that result to os.path.basename().
import os
file_path = "/home/user/documents/report.txt"
filename = os.path.basename(file_path)
parent_directory = os.path.dirname(file_path)
last_directory = os.path.basename(parent_directory)
print(filename)
print(last_directory)
report.txt
documents
Separate a File Name and Extension with os.path.splitext
os.path.splitext() separates the final path component’s extension from the rest of the path and returns a (root, ext) pair. The extension is empty or begins with a period; this function does not return a directory. Use os.path.dirname() when the directory is what you need.
import os
file_path = "/home/user/documents/report.txt"
root, extension = os.path.splitext(file_path)
directory = os.path.dirname(file_path)
print(root)
print(extension)
print(directory)
/home/user/documents/report
.txt
/home/user/documents
For a filename such as archive.tar.gz, splitext() splits off only the final suffix, .gz. The returned root still contains archive.tar, so extension handling should match the naming convention your application expects.
Get a Directory from a File Path with os.path.dirname
os.path.dirname() returns the directory portion of a path as a string. It is the direct choice when you already have a path string and want the parent directory of its final component.
import os
file_path = "/home/user/documents/report.txt"
directory = os.path.dirname(file_path)
print(directory)
/home/user/documents
Use the same variable for the input and function call. The function works on the path text; it does not check whether the file or directory exists. If you need to create, list, or inspect the directory, use the appropriate filesystem operation separately.
Get a Parent Directory with pathlib
pathlib provides Path objects for path operations. The Path.parent attribute returns the parent path as another Path object, which can be convenient when chaining path operations or passing paths to APIs that accept path-like values.
from pathlib import Path
file_path = Path("/home/user/documents/report.txt")
parent_directory = file_path.parent
print(parent_directory)
print(file_path.parts)
/home/user/documents
('/', 'home', 'user', 'documents', 'report.txt')
Path.parts is a tuple containing the path’s components; it is an attribute, not a function. Choose Path.parent for the parent directory, and use parts when you need to examine or index individual components.
Resolve Relative Paths and Get the Current Working Directory
A relative path is interpreted from the process’s current working directory. os.path.abspath() converts a relative path to an absolute path using that working directory. To obtain the containing directory of the resulting path, pass it to os.path.dirname().
import os
relative_path = "documents/report.txt"
absolute_path = os.path.abspath(relative_path)
file_directory = os.path.dirname(absolute_path)
print(absolute_path)
print(file_directory)
/work/documents/report.txt
/work/documents
Path.cwd() returns the current working directory as a Path object, while os.getcwd() returns it as a string. They describe the process’s working directory, which can differ from the directory containing the Python script. Use these when you need to understand where relative paths start; use Path(__file__).parent in a script when you need the directory containing that script.
import os
from pathlib import Path
print(Path.cwd())
print(os.getcwd())
/work
/work