f python?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sun Apr 8 09:29:19 EDT 2012


On Sun, 08 Apr 2012 11:34:56 +0000, Steven D'Aprano wrote:

> When the only tool you know how to use is a hammer, everything looks
> like a nail. Instead of using regexes ("now you have two problems"), use
> the right tool: to count path components, split the path, then count the
> number of path components directly.
> 
> import os
> components = os.path.split(some_path)
> print len(components)

Which is completely wrong. How embarrassing. Serves me right for not 
testing my code before sending.

Nevertheless it is easy enough to write a split path function, or even to 
use it in place:

def splitpath(path):
    return os.path.normpath(path).split(os.path.sep)



-- 
Steven



More information about the Python-list mailing list