Skip to content

Instantly share code, notes, and snippets.

Embed
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
@contextmanager
def visitDir(d):
os.chdir(d)
yield d
os.chdir("..")
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
You can’t perform that action at this time.