Function to create and return a unique temporary directory. Parameters ---------- dir : str If ``dir`` is not None, the directory will be created in that directory; otherwise, Python's default temporary directory is used. Returns ------- out : str
(dir=None)
| 377 | |
| 378 | @contextmanager |
| 379 | def tmpdir(dir=None): |
| 380 | """ |
| 381 | Function to create and return a unique temporary directory. |
| 382 | |
| 383 | Parameters |
| 384 | ---------- |
| 385 | dir : str |
| 386 | If ``dir`` is not None, the directory will be created in that directory; otherwise, |
| 387 | Python's default temporary directory is used. |
| 388 | |
| 389 | Returns |
| 390 | ------- |
| 391 | out : str |
| 392 | Path to the temporary directory |
| 393 | |
| 394 | Notes |
| 395 | ----- |
| 396 | This context manager is particularly useful on Windows for opening temporary directories multiple times. |
| 397 | """ |
| 398 | dirname = tempfile.mkdtemp(dir=dir) |
| 399 | |
| 400 | try: |
| 401 | yield dirname |
| 402 | finally: |
| 403 | if os.path.exists(dirname): |
| 404 | if os.path.isdir(dirname): |
| 405 | with suppress(OSError): |
| 406 | shutil.rmtree(dirname) |
| 407 | else: |
| 408 | with suppress(OSError): |
| 409 | os.remove(dirname) |
| 410 | |
| 411 | |
| 412 | @contextmanager |
searching dependent graphs…