how to determine whether pathname1 is below bathname2

Dan Stromberg drsalists at gmail.com
Sun Jul 11 18:13:54 EDT 2010


You could probably:

cd to dir1
getcwd

cd to dir2
getcwd

repeat
   cd ..
   getcwd
   if getcwd == dir1's cwd, then under
until at /

cd to dir1
repeat
   cd ..
   getcwd
   if getcwd == dir2's cwd, then under
until at /

This should deal with symlinks and junctions, as long as you aren't worried
about permissions issues on directories.

On Sun, Jul 11, 2010 at 7:34 AM, Gelonida <gelonida at gmail.com> wrote:

> Hi Thomas,
>
> Thomas Jollans wrote:
> > On 07/11/2010 03:37 PM, Gelonida wrote:
> >> #################################################
> >> import os
> >> def is_below_dir(fname,topdir):
> >>     relpath = os.path.relpath(fname,topdir)
> >>     return not relpath.startswith('..'+os.sep)
> >>
> >> print is_below_dir(path1,path2)
> >> #################################################
> >> The basic idea is, if the path name of path1
> >> relative to path2 does NOT start with '..', then
> >> it must be below path2
> >>
> >>
> >> Does anybody see pitfalls with that solution?
> >> Is there by any chance a function, that I overlooked,
> >> which does already what I'd like to do?
> >
> > It probably won't work on Windows because it isolates volumes (drive
> > letters). What does, for example, os.path.relpath do when
> > you pass r'c:\foo\bar', r'y:\drive\letters\are\silly' ? I see two
> > reasonably correct options:
> Thanks, Indeed. different drives raise an ecception.
> I could catch the ValueError and let it just return False
>
> >
> > either raise an exception (there is no relative path) or return the
> > absolute path, which doesn't start with ..
> >
> > On UNIX, the only potential problem I see is that it may or may not do
> > what you expect when symlinks are involved somewhere.
>
> Also true. In my case I'd prefer it would not follow, but
> it doesn't really matter.
>
>
> So my function in order to be portable
> had now to look like:
> #################################################
> import os
> def is_below_dir(fname,topdir):
>     try:
>        relpath = os.path.relpath(fname,topdir)
>     except ValueError:
>        return False
>     return not relpath.startswith('..'+os.sep)
>
> print is_below_dir(path1,path2)
> #################################################
>
> if I wanted to folow symlinks, then had to apply
> os.path.realpath() on
> fname AND on topdir
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100711/378b896b/attachment-0001.html>


More information about the Python-list mailing list