Skip to content

tempdir

Temporary directory management with fork safety and fast filesystem support.

This module provides utilities for creating and managing temporary directories, including a per-process root temp dir (safely re-initialized after fork), and :func:fastfs_dir which locates the fastest available filesystem (tmpfs/ramfs) for high-throughput temporary files.

create()

Create a new temporary directory under the process root temp dir.

Returns:

Type Description

The path of the newly created directory.

Example
import os

from py_misc_utils import tempdir as tmpd

sub = tmpd.create()

print(sub.startswith(tmpd.get_temp_root()))  # True
print(os.path.isdir(sub))  # True
Source code in py_misc_utils/tempdir.py
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
def create():
  """Create a new temporary directory under the process root temp dir.

  Returns:
    The path of the newly created directory.

  Example:
    import os

    from py_misc_utils import tempdir as tmpd

    sub = tmpd.create()

    print(sub.startswith(tmpd.get_temp_root()))  # True
    print(os.path.isdir(sub))  # True
  """
  return _root_dir().create()

get_temp_root()

Return the process root temporary directory path.

The root is created on first use and removed at process exit.

Returns:

Type Description

The root temporary directory path.

Example
import os

from py_misc_utils import tempdir as tmpd

root = tmpd.get_temp_root()

print(os.path.isdir(root))  # True
print(tmpd.get_temp_root() == root)  # True
Source code in py_misc_utils/tempdir.py
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
def get_temp_root():
  """Return the process root temporary directory path.

  The root is created on first use and removed at process exit.

  Returns:
    The root temporary directory path.

  Example:
    import os

    from py_misc_utils import tempdir as tmpd

    root = tmpd.get_temp_root()

    print(os.path.isdir(root))  # True
    print(tmpd.get_temp_root() == root)  # True
  """
  return _root_dir().root()

fastfs_dir(name=None, namelen=_NAMELEN)

Create (or retrieve) a subdirectory in the fast filesystem area.

The parent directory is located by checking the FASTFS_DIR environment variable first, then known tmpfs/ramfs locations, the default temp dir and finally the current working directory.

Parameters:

Name Type Description Default
name

Optional directory name; defaults to a random name of namelen chars.

None
namelen

Length of the random name when name is not provided.

_NAMELEN

Returns:

Type Description

The path of the created directory.

Example
import os

from py_misc_utils import tempdir as tmpd

work = tmpd.fastfs_dir(name='myjob')

print(os.path.isdir(work))  # True
Source code in py_misc_utils/tempdir.py
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
def fastfs_dir(name=None, namelen=_NAMELEN):
  """Create (or retrieve) a subdirectory in the fast filesystem area.

  The parent directory is located by checking the FASTFS_DIR environment
  variable first, then known tmpfs/ramfs locations, the default temp dir
  and finally the current working directory.

  Args:
    name: Optional directory name; defaults to a random name of namelen chars.
    namelen: Length of the random name when name is not provided.

  Returns:
    The path of the created directory.

  Example:
    import os

    from py_misc_utils import tempdir as tmpd

    work = tmpd.fastfs_dir(name='myjob')

    print(os.path.isdir(work))  # True
  """
  name = name or rngu.rand_string(namelen)

  path = os.path.join(_FASTFS_DIR, name)
  os.makedirs(path, exist_ok=True)

  return path