MCPcopy Create free account
hub / github.com/aboutcode-org/scancode-toolkit / create_dir

Function create_dir

src/commoncode/fileutils.py:64–97  ·  view source on GitHub ↗

Create directory and all sub-directories recursively at location ensuring these are readable and writeable. Raise Exceptions if it fails to create the directory.

(location)

Source from the content-addressed store, hash-verified

62
63
64def create_dir(location):
65 """
66 Create directory and all sub-directories recursively at location ensuring these
67 are readable and writeable.
68 Raise Exceptions if it fails to create the directory.
69 """
70
71 if os.path.exists(location):
72 if not os.path.isdir(location):
73 err = "Cannot create directory: existing file in the way %(location)s."
74 raise OSError(err % locals())
75 else:
76 # may fail on win if the path is too long
77 # FIXME: consider using UNC ?\\ paths
78
79 try:
80 os.makedirs(location)
81 chmod(location, RW, recurse=False)
82
83 # avoid multi-process TOCTOU conditions when creating dirs
84 # the directory may have been created since the exist check
85 except WindowsError as e:
86 # [Error 183] Cannot create a file when that file already exists
87 if e and e.winerror == 183:
88 if not os.path.isdir(location):
89 raise
90 else:
91 raise
92 except (IOError, OSError) as o:
93 if o.errno == errno.EEXIST:
94 if not os.path.isdir(location):
95 raise
96 else:
97 raise
98
99
100def get_temp_dir(base_dir=_base_temp_dir, prefix=""):

Calls 2

chmodFunction · 0.70
existsMethod · 0.45