How to check whether a directory is a sub directory of another directory
Problems with many of the suggested methods
If you're going to test for directory parentage with string comparison or os.path.commonprefix
methods, these are prone to errors with similarly-named paths or relative paths. For example:
/path/to/files/myfile
would be shown as a child path of/path/to/file
using many of the methods./path/to/files/../../myfiles
would not be shown as a parent of/path/myfiles/myfile
by many of the methods. In fact, it is.
The previous answer by Rob Dennis provides a good way to compare path parentage without encountering these problems. Python 3.4 added the pathlib
module which can perform these kind of path operations in a more sophisticated way, optionally without referencing the underlying OS. jme has described in another previous answer how to use pathlib
for the purpose of accurately determining if one path is a child of another. If you prefer not to use pathlib
(not sure why, it's pretty great) then Python 3.5 introduced a new OS-based method in os.path
that allows you to do perform path parent-child checks in a similarly accurate and error-free manner with a lot less code.
New for Python 3.5
Python 3.5 introduced the function os.path.commonpath
. This is a method that is specific to the OS that the code is running on. You can use commonpath
in the following way to accurately determine path parentage:
def path_is_parent(parent_path, child_path):
# Smooth out relative path names, note: if you are concerned about symbolic links, you should use os.path.realpath too
parent_path = os.path.abspath(parent_path)
child_path = os.path.abspath(child_path)
# Compare the common path of the parent and child path with the common path of just the parent path. Using the commonpath method on just the parent path will regularise the path name in the same way as the comparison that deals with both paths, removing any trailing path separator
return os.path.commonpath([parent_path]) == os.path.commonpath([parent_path, child_path])
Accurate one-liner
You can combine the whole lot into a one-line if statement in Python 3.5. It's ugly, it includes unnecessary duplicate calls to os.path.abspath
and it definitely won't fit in the PEP 8 79-character line-length guidelines, but if you like that kind of thing, here goes:
if os.path.commonpath([os.path.abspath(parent_path_to_test)]) == os.path.commonpath([os.path.abspath(parent_path_to_test), os.path.abspath(child_path_to_test)]):
# Yes, the child path is under the parent path
New for Python 3.9
pathlib
has a new method on PurePath
called is_relative_to
which performs this function directly. You can read the python documentation on how is_relative_to
works if you need to see how to use it. Or you can see my other answer for a more full description of how to use it.
Python 3's pathlib
module makes this straightforward with its Path.parents attribute. For example:
from pathlib import Path
root = Path('/path/to/root')
child = root / 'some' / 'child' / 'dir'
other = Path('/some/other/path')
Then:
>>> root in child.parents
True
>>> other in child.parents
False