Creates a temporary directory and sets the cwd to that directory. Automatically reverts to previous cwd upon cleanup. Usage example: with TemporaryWorkingDirectory() as tmpdir: ...
| 43 | |
| 44 | |
| 45 | class TemporaryWorkingDirectory(TemporaryDirectory): |
| 46 | """ |
| 47 | Creates a temporary directory and sets the cwd to that directory. |
| 48 | Automatically reverts to previous cwd upon cleanup. |
| 49 | Usage example: |
| 50 | |
| 51 | with TemporaryWorkingDirectory() as tmpdir: |
| 52 | ... |
| 53 | """ |
| 54 | |
| 55 | def __enter__(self) -> str: |
| 56 | self.old_wd = Path.cwd() |
| 57 | _os.chdir(self.name) |
| 58 | return super(TemporaryWorkingDirectory, self).__enter__() |
| 59 | |
| 60 | def __exit__(self, exc: Optional[Type[BaseException]], value: Optional[BaseException], tb: Optional[TracebackType]) -> None: |
| 61 | _os.chdir(self.old_wd) |
| 62 | return super(TemporaryWorkingDirectory, self).__exit__(exc, value, tb) |
no outgoing calls
searching dependent graphs…