Safely create a directory. Ensures a directory is present. If it's not there, it is created. If it is, it's a no-op. If clean is True, ensures the directory is empty.
(directory, clean=False)
| 569 | |
| 570 | |
| 571 | def safe_mkdir(directory, clean=False): |
| 572 | # type: (_Text, bool) -> _Text |
| 573 | """Safely create a directory. |
| 574 | |
| 575 | Ensures a directory is present. If it's not there, it is created. If it is, it's a no-op. If |
| 576 | clean is True, ensures the directory is empty. |
| 577 | """ |
| 578 | if clean: |
| 579 | safe_rmtree(directory) |
| 580 | try: |
| 581 | os.makedirs(directory) |
| 582 | except OSError as e: |
| 583 | if e.errno != errno.EEXIST: |
| 584 | raise |
| 585 | return directory |
| 586 | |
| 587 | |
| 588 | def safe_open(filename, *args, **kwargs): |