Created
December 2, 2014 18:59
-
-
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
This file contains hidden or 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, 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