0

I have a path like so:

S:\Test\Testing\Tested\A\B\C

and a list

include = ['S:\Test\Testing',
           'S:\Domino\Testing',
           'S:\Money\tmp']

How do I check if the path I have starts with any of the paths in this list?

So in this case the first element would match and it would return True.

6
  • 1
    Relevant standard library module: docs.python.org/3/library/pathlib.html Commented Jan 24, 2023 at 19:12
  • something like mypath.startswith(beginning+os.sep) ? Commented Jan 24, 2023 at 19:12
  • How do you determine that you want S:\Test\Testing from S:\Test\Testing\Tested\A\B\C? Do you want to extract the path exactly three levels deep, or the one to the directory named Testing however deep, or some other criteria? Commented Jan 24, 2023 at 19:20
  • I just want the first 3 elements of the path to see if another path begins with those 3 elements Commented Jan 24, 2023 at 19:22
  • I've updated the question to be more clear Commented Jan 24, 2023 at 19:26

3 Answers 3

1

A way to do it without worrying about manual string comparisons:

from pathlib import PurePath

original_parent_path = PurePath(r"S:\Test\Testing\removed\sub\dirs")

# Only take top 3 directories (S:\ counts as one)
parent_path = PurePath(*original_parent_path.parts[:3])

test_path1 = PurePath(r"S:\Test\Testing\subdir")
test_path2 = PurePath(r"S:\Test\notsubdir")

print(test_path1.is_relative_to(parent_path)) # True
print(test_path2.is_relative_to(parent_path)) # False

PurePath makes no access to the filesystem so this does not check if the directories actually exist.

Sign up to request clarification or add additional context in comments.

6 Comments

Sorry this solution is great. I updated my question however to check from a list.
@Eisen just iterate over the list! it may be practical to use a for:else style where you break on success and raise in else .. or just pack it all into a function and return
"PurePath makes no access to the filesystem so this does not check if the directories actually exist." It's worth noting that using PurePath isn't import for not accessing the filesystem. Creating a concrete path with Path also wouldn't access the filesystem, and likewise does not check if the directory exists. It's just that concrete paths provide methods for accessing the filesystem, while pure paths don't.
@Eisen just iterate over the list until you find one that is a parent directory, also be careful to add r before strings with backslashes in them if you don't want them to mean an escape sequence.
Be mindful that this example won't run as expected on a non-Windows system, where PurePath(r"S:\Test\Testing\removed\sub\dirs") will be a path with only one part. Using PureWindowsPath explicitly would fix this
|
1

Use .commonpath() from the os.path module:

import os

p1 = 'S:\Test\Testing\Tested\A\B\C'
p2 = 'S:\Test\Testing\Hello'

common_path = os.path.commonpath([p1, p2])
common_path

output:

'S:\Test\Testing'
include = ['S:\Test\Testing',
           'S:\Domino\Testing',
           'S:\Money\tmp']

common_path in include

output:

True

Comments

0

Using pathlib:

from pathlib import PureWindowsPath as Path

start_path = Path(r"S:\Test\Testing\Tested\A\B\C")

three_deep_path = Path(*start_path.parts[:3])
print(three_deep_path)

test_path = Path(r"S:\Test\Testing\Hello")
print(test_path.is_relative_to(base_path))

bad_path = Path(r"S:\Test\NotTesting\Hello")
print(bad_path.is_relative_to(base_path))

outputs

S:\Test\Testing
True
False

1 Comment

I like this solution!!! I updated my question however

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.