-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathkernel_util.py
More file actions
35 lines (30 loc) · 998 Bytes
/
Copy pathkernel_util.py
File metadata and controls
35 lines (30 loc) · 998 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import os
import errno
def safe_mkdir(path, verbose=False):
"""
Create a folder and catch the race condition between path exists and mkdir.
Parameters
----------
path : string
Name of the folder to be created (can be full path).
verbose : bool, optional
If True, print messages about the status. Default is False.
Examples
----------
Folders are created
>>> safe_mkdir('toto/titi/tutu', verbose=True)
Folders aren't created because they already exist
>>> safe_mkdir('toto/titi/tutu', verbose=True)
Folders toto/titi/tutu already exist. Not created.
>>> os.removedirs('toto/titi/tutu')
"""
abspath = os.path.abspath(path)
if not os.path.exists(abspath):
try:
os.makedirs(abspath)
except OSError as exception:
if exception.errno != errno.EEXIST:
raise
else:
if verbose:
print("Folders {} already exist. Not created.".format(path))