f python?
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Sun Apr 8 07:34:56 EDT 2012
On Sun, 08 Apr 2012 04:11:20 -0700, Xah Lee wrote:
[...]
I have read Xah Lee's post so that you don't have to.
Shorter Xah Lee:
"I don't know Python very well, and rather than admit I made
some pretty lousy design choices in my code, I blame Python.
And then I cross-post about it, because I'm the most important
person in the Universe."
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)
No matter what separator the OS users, os.path.split will do the right
thing. There's no need to mess about escaping separators so you can
hammer it with the regex module, when Python comes with the perfectly
functional socket-wrench you actually need.
--
Steven
More information about the Python-list
mailing list