[R] Is there a function that can test if a path is in a directory or one of its sub-directory (recursively)?

Peng Yu pengyu.ut at gmail.com
Fri Nov 6 16:36:19 CET 2009


On Fri, Nov 6, 2009 at 12:49 AM, Gabriel Genellina
<gagsl-py2 at yahoo.com.ar> wrote:
> En Fri, 06 Nov 2009 00:53:14 -0300, Chris Rebert <clp2 at rebertia.com>
> escribió:
>>
>> On Thu, Nov 5, 2009 at 7:41 PM, Peng Yu <pengyu.ut at gmail.com> wrote:
>
>>> I looked though the os.path manual. I don't find a function that can
>>> test if a path is in a directory or its sub-directory (recursively).
>>>
>>> For example, /a/b/c/d is in /a its sub-directory (recursively). Could
>>> somebody let me know if such function is available somewhere?
>>
>> Couldn't you just canonicalize the paths using os.path.abspath() and
>> friends and then do subdirectory.startswith(parent_directory) [as
>> strings]?
>
> Beware of directories starting with the same letters.
> I'd canonicalize, split the strings on os.sep, and compare the resulting
> lists up to the shortest one.

Just to add one more comment. There are other corner cases where there
is a symbolic link in the path. 'abspath()' may not be good for the
corner cases.




More information about the R-help mailing list