Search code examples
python-3.xpathlib

Does Path Object have builtin method to test if directory is in the Path


Is there a way to check if a directory is in the path of a python Path object. I know I can test it by converting it to a string like 'dir' in str(Path('/dir/dir2/dir3')) or 'dir' in Path('/dir/dir2/dir3').parts but I'm hoping there is a built in method. I've checked the documentation but didn't see anything that is a builtin method that would do this.


Solution

  • from pathlib import Path
    p=Path('/dir/dir2/dir3')
    if p.match("*dir*"):
        print("found 'dir' in path")
    

    This should find if dir is anywhere in the given path.