MCPcopy Index your code
hub / github.com/python/cpython / mkdtemp

Function mkdtemp

Lib/tempfile.py:357–399  ·  view source on GitHub ↗

User-callable function to create and return a unique temporary directory. The return value is the pathname of the directory. Arguments are as for mkstemp, except that the 'text' argument is not accepted. The directory is readable, writable, and searchable only by the creating

(suffix=None, prefix=None, dir=None)

Source from the content-addressed store, hash-verified

355
356
357def mkdtemp(suffix=None, prefix=None, dir=None):
358 """User-callable function to create and return a unique temporary
359 directory. The return value is the pathname of the directory.
360
361 Arguments are as for mkstemp, except that the 'text' argument is
362 not accepted.
363
364 The directory is readable, writable, and searchable only by the
365 creating user.
366
367 Caller is responsible for deleting the directory when done with it.
368 """
369
370 prefix, suffix, dir, output_type = _sanitize_params(prefix, suffix, dir)
371
372 names = _get_candidate_names()
373 if output_type is bytes:
374 names = map(_os.fsencode, names)
375
376 for seq in range(TMP_MAX):
377 name = next(names)
378 file = _os.path.join(dir, prefix + name + suffix)
379 _sys.audit("tempfile.mkdtemp", file)
380 try:
381 _os.mkdir(file, 0o700)
382 except FileExistsError:
383 continue # try again
384 except PermissionError:
385 # On Posix, this exception is raised when the user has no
386 # write access to the parent directory.
387 # On Windows, it is also raised when a directory with
388 # the chosen name already exists, or if the parent directory
389 # is not a directory.
390 # We cannot distinguish between "directory-exists-error" and
391 # "access-denied-error".
392 if _os.name == 'nt' and _os.path.isdir(dir) and seq < TMP_MAX - 1:
393 continue
394 else:
395 raise
396 return _os.path.abspath(file)
397
398 raise FileExistsError(_errno.EEXIST,
399 "No usable temporary directory name found")
400
401def mktemp(suffix="", prefix=template, dir=None):
402 """User-callable function to return a unique temporary file name. The

Callers 1

__init__Method · 0.85

Calls 6

_sanitize_paramsFunction · 0.85
_get_candidate_namesFunction · 0.85
joinMethod · 0.45
mkdirMethod · 0.45
isdirMethod · 0.45
abspathMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…