The "path" module
Somewhat belatedly I’ve started using Jason Orendorff’s path module for Python. It’s great. Here’s a comparison with the stock os.path facilities, grabbed from Jason’s site:
# with os.path.walk
def delete_backups(arg, dirname, names):
for name in names:
if name.endswith('~'):
os.remove(os.path.join(dirname, name))
os.path.walk(os.environ['HOME'], delete_backups, None)
# with path
d = path(os.environ['HOME'])
for f in d.walkfiles('*~'):
f.remove()
The second snippet is not just shorter, it’s easier to read and easier to write. I’m writing some code to recursively process a tree of short text files (Blosxom entries), and the path
module is a godsend. If you’re curious, see the description and examples on Jason’s site; he also posted some interesting comments on Ian Bicking’s blog about the design of the module.