Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

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

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


pengyu.ut at gmail

Nov 5, 2009, 7:41 PM

Post #1 of 5 (245 views)
Permalink
Is there a function that can test if a path is in a directory or one of its sub-directory (recursively)?

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?
--
http://mail.python.org/mailman/listinfo/python-list


clp2 at rebertia

Nov 5, 2009, 7:53 PM

Post #2 of 5 (225 views)
Permalink
Re: Is there a function that can test if a path is in a directory or one of its sub-directory (recursively)? [In reply to]

On Thu, Nov 5, 2009 at 7:41 PM, Peng Yu <pengyu.ut [at] gmail> 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]?

Cheers,
Chris
--
http://blog.rebertia.com
--
http://mail.python.org/mailman/listinfo/python-list


gagsl-py2 at yahoo

Nov 5, 2009, 10:49 PM

Post #3 of 5 (219 views)
Permalink
Re: Is there a function that can test if a path is in a directory or one of its sub-directory (recursively)? [In reply to]

En Fri, 06 Nov 2009 00:53:14 -0300, Chris Rebert <clp2 [at] rebertia>
escribió:
> On Thu, Nov 5, 2009 at 7:41 PM, Peng Yu <pengyu.ut [at] gmail> 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.

--
Gabriel Genellina

--
http://mail.python.org/mailman/listinfo/python-list


clp2 at rebertia

Nov 6, 2009, 12:19 AM

Post #4 of 5 (217 views)
Permalink
Re: Is there a function that can test if a path is in a directory or one of its sub-directory (recursively)? [In reply to]

On Thu, Nov 5, 2009 at 10:49 PM, Gabriel Genellina
<gagsl-py2 [at] yahoo> wrote:
> En Fri, 06 Nov 2009 00:53:14 -0300, Chris Rebert <clp2 [at] rebertia>
> escribió:
>> On Thu, Nov 5, 2009 at 7:41 PM, Peng Yu <pengyu.ut [at] gmail> 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.

Ah, I thought there was some edge case I must've missed; my solution
seemed too simple.

Cheers,
Chris
--
http://mail.python.org/mailman/listinfo/python-list


davea at ieee

Nov 6, 2009, 1:08 PM

Post #5 of 5 (211 views)
Permalink
Re: Is there a function that can test if a path is in a directory or one of its sub-directory (recursively)? [In reply to]

Chris Rebert wrote:
> On Thu, Nov 5, 2009 at 10:49 PM, Gabriel Genellina
> <gagsl-py2 [at] yahoo> wrote:
>
>> En Fri, 06 Nov 2009 00:53:14 -0300, Chris Rebert <clp2 [at] rebertia>
>> escribió:
>>
>>> On Thu, Nov 5, 2009 at 7:41 PM, Peng Yu <pengyu.ut [at] gmail> 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.
>>
>
> Ah, I thought there was some edge case I must've missed; my solution
> seemed too simple.
>
> Cheers,
> Chris
>
>
But you can probably add a trailing '/' (os.sep) to the shorter string
before doing the startswith().

DaveA
--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.