0

I have a script where I'm walking through a list of passed (via a csv file) paths. I'm wondering how I can determine if the path I'm currently working with has been previously managed (as a subdirectory of the parent).

I'm keeping a list of managed paths like this:

pathsManaged = ['/a/b', '/c/d', '/e']

So when/if the next path is '/e/a', I want to check in the list if a parent of this path is present in the pathsManaged list.

My attempt so far:

if any(currPath in x for x in pathsManaged):
   print 'subdir of already managed path'

This doesn't seem to be working though. Am I expecting too much from the any command. Are there any other shortcuts that I could use for this type of look-up?

Thanks

1
  • What is currPath? Could you give few examples and expected result? Commented Mar 28, 2014 at 10:11

3 Answers 3

2

Perhaps:

from os.path import dirname

def parents(p):
    while len(p) > 1:
        p = dirname(p)
        yield p

pathsManaged = ['/a/b', '/c/d', '/e']

currPath = '/e/a'

if any(p in pathsManaged for p in parents(currPath)):
    print 'subdir of already managed path'

prints:

subdir of already managed path
Sign up to request clarification or add additional context in comments.

Comments

0

Assuming that pathsManaged contains absolute paths (otherwise I think all bets are off), then you could make currPath an absolute path and see if it starts with any of the paths in pathsManaged. Or in Python:

def is_path_already_managed(currPath):
    return any(
        os.path.abspath(currPath).startswith(managed_path)
        for managed_path in pathsManaged)

Also conceptually I feel pathsManaged should be a set, not a list.

3 Comments

Thanks. As I'm relatively new to python, I'm curious to know why a set would be preferred over a list in this case.
A list works just fine too, but as it should never contain the same path twice, order doesn't matter, and the only thing that really matters is "is this path in pathsManaged or not", the concept that comes closest to that is a set.
Thanks RemcoGerlich, I'll be using this advice next time.
0

If I understand you correctly, you want to check if any of pathsManaged is part of currPath, but you are doing this other way around. Depending on what you want, one of this should work for you:

any(x in currPath for x in pathsManaged)

any(currPath.startswith(x) for x in pathsManaged)

os.path.dirname(currPath) in pathsManaged

2 Comments

If currPath is '/e/a' then this will return True if there's '/' or '/a' in the list..
if only start of path should be taken into account, any(currPath.startswith(x) for x in pathsManaged) might be better, or os.path.dirname(currPath) in pathsManaged, but I am not sure what exactly OP expects.

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.