Please help me finding a way to implement os.path.issubpath(a, b)

G

Giampaolo Rodola'

Hi,
I'm trying to implement a function which returns whether a path is a
subpath of another one (e.g. /a/b/c is a subpath of /a/b).
I wrote this function which apparently seems to work fine:

import os

def issubpath(path1, path2):
"""Return True if path1 is a sub path of path2."""
if path1 == path2:
return False
x1 = path1.split(os.sep)
x2 = path2.split(os.sep)
return x1[:len(x2)] == x2

....but if I use "issubpath('C:\\dir', 'C:\\')" it returns False.
A little help would be appreciated.

Thanks in advance.


--- Giampaolo
http://code.google.com/p/pyftpdlib/
 
A

Arnaud Delobelle

Hi,
I'm trying to implement a function which returns whether a path is a
subpath of another one (e.g. /a/b/c is a subpath of /a/b).
I wrote this function which apparently seems to work fine:

import os

def issubpath(path1, path2):
    """Return True if path1 is a sub path of path2."""
    if path1 == path2:
        return False
    x1 = path1.split(os.sep)
    x2 = path2.split(os.sep)
    return x1[:len(x2)] == x2

...but if I use "issubpath('C:\\dir', 'C:\\')" it returns False.
A little help would be appreciated.

Thanks in advance.

That's because:
'C:\\dir'.split('\\') ['C:', 'dir']
'C:\\'.split('\\')
['C:', '']

So you could write instead something like

x1 = path1.rstrip(os.sep).split(os.sep)
x2 = path2.rstrip(os.sep).split(os.sep)

in your function

HTH
 
D

Diez B. Roggisch

Giampaolo said:
Hi,
I'm trying to implement a function which returns whether a path is a
subpath of another one (e.g. /a/b/c is a subpath of /a/b).
I wrote this function which apparently seems to work fine:

import os

def issubpath(path1, path2):
"""Return True if path1 is a sub path of path2."""
if path1 == path2:
return False
x1 = path1.split(os.sep)
x2 = path2.split(os.sep)
return x1[:len(x2)] == x2

...but if I use "issubpath('C:\\dir', 'C:\\')" it returns False.
A little help would be appreciated.

Any reason why

os.path.normpath(a).startswith(os.normpath(b)) doesn't do the trick?

Diez
 
F

Fredrik Lundh

Diez said:
Any reason why

os.path.normpath(a).startswith(os.normpath(b)) doesn't do the trick?

Except for the trivial type, you mean? That depends on whether "c:\foo"
should be seen as a subpath to "c:\foobar" or not. I'd probably go for
(also untested):

def issubpath(a, b):
def fixpath(p):
return os.path.normpath(p) + os.sep
return fixpath(a).startswith(fixpath(b))

</F>
 
G

Giampaolo Rodola'

Hi,
I'm trying to implement a function which returns whether a path is a
subpath of another one (e.g. /a/b/c is a subpath of /a/b).
I wrote this function which apparently seems to work fine:
import os
def issubpath(path1, path2):
    """Return True if path1 is a sub path of path2."""
    if path1 == path2:
        return False
    x1 = path1.split(os.sep)
    x2 = path2.split(os.sep)
    return x1[:len(x2)] == x2
...but if I use "issubpath('C:\\dir', 'C:\\')" it returns False.
A little help would be appreciated.
Thanks in advance.

That's because:
'C:\\dir'.split('\\') ['C:', 'dir']
'C:\\'.split('\\')

['C:', '']

So you could write instead something like

    x1 = path1.rstrip(os.sep).split(os.sep)
    x2 = path2.rstrip(os.sep).split(os.sep)

in your function

HTH

Thanks, it seems to work just fine.


--- Giampaolo
http://code.google.com/p/pyftpdlib/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top