<div><br></div>You could probably:<div><br></div><div>cd to dir1</div><div>getcwd</div><div><br></div><div>cd to dir2</div><div>getcwd<br><br></div><div>repeat</div><div>   cd ..</div><div>   getcwd</div><div>   if getcwd == dir1's cwd, then under</div>
<div>until at /</div><div><br></div><div><div>cd to dir1</div></div><div><div>repeat</div><div>   cd ..</div><div>   getcwd</div><div>   if getcwd == dir2's cwd, then under</div><div>until at /</div></div><div><br></div>
<div>This should deal with symlinks and junctions, as long as you aren't worried about permissions issues on directories.</div><div><br><div class="gmail_quote">On Sun, Jul 11, 2010 at 7:34 AM, Gelonida <span dir="ltr"><<a href="mailto:gelonida@gmail.com">gelonida@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">Hi Thomas,<br>
<div class="im"><br>
Thomas Jollans wrote:<br>
> On 07/11/2010 03:37 PM, Gelonida wrote:<br>
>> #################################################<br>
>> import os<br>
>> def is_below_dir(fname,topdir):<br>
>>     relpath = os.path.relpath(fname,topdir)<br>
>>     return not relpath.startswith('..'+os.sep)<br>
>><br>
>> print is_below_dir(path1,path2)<br>
>> #################################################<br>
>> The basic idea is, if the path name of path1<br>
>> relative to path2 does NOT start with '..', then<br>
>> it must be below path2<br>
>><br>
>><br>
>> Does anybody see pitfalls with that solution?<br>
>> Is there by any chance a function, that I overlooked,<br>
>> which does already what I'd like to do?<br>
><br>
> It probably won't work on Windows because it isolates volumes (drive<br>
> letters). What does, for example, os.path.relpath do when<br>
> you pass r'c:\foo\bar', r'y:\drive\letters\are\silly' ? I see two<br>
> reasonably correct options:<br>
</div>Thanks, Indeed. different drives raise an ecception.<br>
I could catch the ValueError and let it just return False<br>
<div class="im"><br>
><br>
> either raise an exception (there is no relative path) or return the<br>
> absolute path, which doesn't start with ..<br>
><br>
> On UNIX, the only potential problem I see is that it may or may not do<br>
> what you expect when symlinks are involved somewhere.<br>
<br>
</div>Also true. In my case I'd prefer it would not follow, but<br>
it doesn't really matter.<br>
<br>
<br>
So my function in order to be portable<br>
had now to look like:<br>
<div class="im">#################################################<br>
import os<br>
def is_below_dir(fname,topdir):<br>
</div>    try:<br>
<div class="im">       relpath = os.path.relpath(fname,topdir)<br>
</div>    except ValueError:<br>
        return False<br>
<div class="im">    return not relpath.startswith('..'+os.sep)<br>
<br>
print is_below_dir(path1,path2)<br>
#################################################<br>
<br>
</div>if I wanted to folow symlinks, then had to apply<br>
os.path.realpath() on<br>
fname AND on topdir<br>
<div><div></div><div class="h5"><br>
<br>
--<br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
</div></div></blockquote></div><br></div>