Monday 22 March 2010 — This is close to 15 years old. Be careful.
Most of the Python standard library is great, providing functions and classes that do their jobs well, often even before you knew you needed the job done (urlsafe_b64encode FTW!)
Which makes my disappointment with os.path.commonprefix all the stronger. This function is worse than useless, it’s misleading. Although it’s in the os.path module, it knows nothing about paths, working instead character-by-character:
>>> os.path.commonprefix(['/home/ned/cog', '/home/ned/coverage'])
'/home/ned/co' # That's not an actual path!
The docs helpfully include the warning:
Note that this may return invalid paths because it works a character at a time.
But it should say:
This function is in the wrong place, and has nothing to do with paths, don’t use it if you are interested in file paths!
I accepted a patch to coverage.py which used this function, and it looked good. But eventually I turned up cases it got wrong, and had to re-discover what people seem to have understood this for at least eight years. *Sigh*
Comments
It does exactly what I needed at the time, but breaks horribly in almost every other case. I keep meaning to go back and write it properly, but...
Of course, I've not released it as part of a standard library.
Add a comment: