mirror of
https://github.com/aykhans/AzSuicideDataVisualization.git
synced 2025-04-21 18:23:35 +00:00
21 lines
482 B
Python
21 lines
482 B
Python
"""Backport of Python 3.11's contextlib.chdir."""
|
|
|
|
|
|
import os
|
|
from contextlib import AbstractContextManager
|
|
|
|
|
|
class chdir(AbstractContextManager):
|
|
"""Non thread-safe context manager to change the current working directory."""
|
|
|
|
def __init__(self, path):
|
|
self.path = path
|
|
self._old_cwd = []
|
|
|
|
def __enter__(self):
|
|
self._old_cwd.append(os.getcwd())
|
|
os.chdir(self.path)
|
|
|
|
def __exit__(self, *excinfo):
|
|
os.chdir(self._old_cwd.pop())
|