[Python-ideas] os.path.isparent and os.path.ischild
Steven D'Aprano
steve at pearwood.info
Fri Jul 8 11:07:05 CEST 2011
Andrew Bennetts wrote:
> Steven D'Aprano wrote:
>> I'm not sure that this is so common and useful that it needs to be
>> included in os.path. You can always just add it to your own library,
>> or even write them in-line: the implementation is just a one-liner.
>>
>> def isparent(path1, path2):
>> "Returns True if path1 is a parent of path2."
>> return os.path.commonprefix([path1, path2]) == path1
>>
>> def ischild(path1, path2):
>> "Returns True if path1 is a child of path2."
>> return os.path.commonprefix([path1, path2]) == path2
>
> Those one-liners are simple, obvious, and dangerously wrong:
>
>>>> ischild('aa', 'a')
> True
I would call that a bug in commonprefix.
>>> os.path.commonprefix(['/dir/pics/file', '/dir/pictures/file'])
'/dir/pic'
commonprefix is documented as returning the longest common path
component, not leading substring:
>>> help(os.path.commonprefix)
commonprefix(m)
Given a list of pathnames, returns the longest common leading
component
--
Steven
More information about the Python-ideas
mailing list