contextmanager (1) 썸네일형 리스트형 [Python] with 컨텍스트를 이용해서 다른 디렉토리에서 작업하고 오기. ref : https://stackoverflow.com/questions/299446/how-do-i-change-directory-back-to-my-original-working-directory-with-python 컨텍스트 매니저 함수를 다음과 같이 정의하고, from contextlib import contextmanager @contextmanager def cwd(path): oldpwd=os.getcwd() os.chdir(path) try: yield finally: os.chdir(oldpwd) 다음과 같이 쓰면 된다. print('current dir = `%s`'%(os.getcwd()) with cwd('./subdir'): # 여기서 하고 싶은 일들.. 이전 1 다음