Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Trying to use Python context managers and streams together, but it doesn't work
import os, sys
from contextlib import contextmanager
from glob import glob
def visitDir(d):
yield d
paths = [os.path.join('.', p[0:-1]) for p in glob('*/')]
def clean():
for p in (visitDir(p) for p in paths): # This is the magic line
print p
print os.getcwd()
clean() # Context manager apparently expires within the generator expression
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment