Skip to content

Instantly share code, notes, and snippets.

@BruceEckel
Created December 2, 2014 18:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BruceEckel/e3eb11e5b18fc13a28f8 to your computer and use it in GitHub Desktop.
Save BruceEckel/e3eb11e5b18fc13a28f8 to your computer and use it in GitHub Desktop.
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