Python path module
22nd January 2003
The path Python module (via The Daily Python-URL) is a nice looking wrapper class for Python’s oft-confusing os.path module. Check out this neat code comparison:
# 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 os.path, if (like me) you can never remember how os.path.walk works
def walk_tree_delete_backups(dir):
for name in os.listdir(dir):
path = os.path.join(dir, name)
if os.path.isdir(path):
walk_tree_delete_backups(path)
elif name.endswith('~'):
os.remove(path)
walk_tree_delete_backups(os.environ['HOME'])
# with path
dir = path(os.environ['HOME'])
for f in dir.walk():
if f.isfile() and f.endswith('~'):
os.remove(f)
I’d go for the third one any day.
More recent articles
- Changes in the system prompt between Claude Opus 4.6 and 4.7 - 18th April 2026
- Join us at PyCon US 2026 in Long Beach - we have new AI and security tracks this year - 17th April 2026
- Qwen3.6-35B-A3B on my laptop drew me a better pelican than Claude Opus 4.7 - 16th April 2026