path.startswith(dir)?

Magnus Lie Hetland mlh at vier.idi.ntnu.no
Sat May 11 11:04:14 EDT 2002


In article <HPVC8.64418$m26.31933 at atlpnn01.usenetserver.com>, Steve
Holden wrote:
>"Magnus Lie Hetland" <mlh at vier.idi.ntnu.no> wrote ...
>> I've got some code which tests for
>>
>>   commonprefix([dir, filename]) == dir
>>
>> Assuming that dir ends with a separator (e.g. '/'), it ought to be
>> just as safe (and more concise) to use
>>
>>   filename.startswith(dir)
>>
>> no?
>>
>> And -- ensuring that dir ends with a separator can be done with
>>
>>   dir = join(dir, '')
>>
>> This ought to be a safe/platform independent way of doing it, right?
>>
>
>The safe, platform-independent way to do it is to use the functions provided
>in the os.path module. This should ensure that if someone comes up with a
>new way (in a new OS, of course) of describing file paths, you won't have to
>make changes to your code.

The functions above are from the os.path module. Both filename and
dir have been passed through abspath. In light of this fact, I think
the use of the string method startswith is safe -- which is what I
said in the original mail. I don't see any reason why it shouldn't be.
Do you? (That's what I was asking, really :)

>regards
> Steve

--
Magnus Lie Hetland                                  The Anygui Project
http://hetland.org                                  http://anygui.org



More information about the Python-list mailing list