Created
March 23, 2012 21:13
-
-
Save btimby/2175107 to your computer and use it in GitHub Desktop.
os.walk() clone that uses Django storage object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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