path.startswith(dir)?

Steve Holden sholden at holdenweb.com
Fri May 10 16:07:27 EDT 2002


"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.

regards
 Steve
--
-----------------------------------------------------------------------
Steve Holden                                 http://www.holdenweb.com/
Python Web Programming                http://pydish.holdenweb.com/pwp/
-----------------------------------------------------------------------








More information about the Python-list mailing list