Skip to content

Instantly share code, notes, and snippets.

@btimby
Created March 23, 2012 21:13
Show Gist options
  • Save btimby/2175107 to your computer and use it in GitHub Desktop.
Save btimby/2175107 to your computer and use it in GitHub Desktop.
os.walk() clone that uses Django storage object
import os
def walk(storage, top='/', topdown=False, onerror=None):
"""An implementation of os.walk() which uses the Django storage for
listing directories."""
try:
dirs, nondirs = storage.listdir(top)
except os.error, err:
if onerror is not None:
onerror(err)
return
if topdown:
yield top, dirs, nondirs
for name in dirs:
new_path = os.path.join(top, name)
for x in walk(storage, new_path):
yield x
if not topdown:
yield top, dirs, nondirs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment