tempfile

The tempfile module provides utilities for creating temporary files and directories. It mirrors Python's tempfile API with three classes: TempFile for low-level operations, NamedTemporaryFile for temporary files with automatic cleanup, and TemporaryDirectory for temporary directories with recursive cleanup.

import "tempfile" for TempFile, NamedTemporaryFile, TemporaryDirectory

TempFile Class

TempFile

Low-level temporary file and directory utilities

Static Properties

TempFile.tempdir → String|Null

Gets or sets a global override for the temporary directory. When set, gettempdir() returns this value instead of checking environment variables.

TempFile.tempdir = "/my/custom/tmp"
System.print(TempFile.gettempdir())  // /my/custom/tmp
TempFile.tempdir = null              // Reset to default

Static Methods

TempFile.gettempdir() → String

Returns the resolved temporary directory path. Checks in order: the tempdir override, then TMPDIR, TEMP, TMP environment variables, and falls back to /tmp on POSIX or C:\Temp on Windows.

TempFile.gettempprefix() → String

Returns the default prefix for temporary file names: "tmp".

TempFile.mktemp() → String
TempFile.mktemp(suffix) → String
TempFile.mktemp(suffix, prefix) → String
TempFile.mktemp(suffix, prefix, dir) → String

Generates a unique temporary file path without creating any file. The generated name consists of the prefix, 8 random hexadecimal characters, and the suffix.

  • suffix (String) - Suffix for the filename (default: "")
  • prefix (String) - Prefix for the filename (default: "tmp")
  • dir (String) - Directory for the path (default: gettempdir())
  • Returns: A file path string (file is not created)
var name = TempFile.mktemp(".txt", "data_")
System.print(name)  // e.g., /tmp/data_a1b2c3d4.txt
TempFile.mkstemp() → String
TempFile.mkstemp(suffix) → String
TempFile.mkstemp(suffix, prefix) → String
TempFile.mkstemp(suffix, prefix, dir) → String

Creates a temporary file atomically using exclusive creation flags (O_EXCL), preventing race conditions. Retries up to 100 times on name collisions.

  • suffix (String) - Suffix for the filename (default: "")
  • prefix (String) - Prefix for the filename (default: "tmp")
  • dir (String) - Directory for the file (default: gettempdir())
  • Returns: The path of the newly created file
import "io" for File

var path = TempFile.mkstemp(".dat", "work_")
System.print(path)  // e.g., /tmp/work_e5f6a7b8.dat
File.delete(path)   // Caller must clean up
TempFile.mkdtemp() → String
TempFile.mkdtemp(suffix) → String
TempFile.mkdtemp(suffix, prefix) → String
TempFile.mkdtemp(suffix, prefix, dir) → String

Creates a temporary directory. Retries up to 100 times on name collisions.

  • suffix (String) - Suffix for the directory name (default: "")
  • prefix (String) - Prefix for the directory name (default: "tmp")
  • dir (String) - Parent directory (default: gettempdir())
  • Returns: The path of the newly created directory
import "io" for Directory

var dir = TempFile.mkdtemp("_work")
System.print(dir)       // e.g., /tmp/tmp9c0d1e2f_work
Directory.delete(dir)   // Caller must clean up

NamedTemporaryFile Class

NamedTemporaryFile

A temporary file with a visible name that is optionally deleted on close

Constructors

NamedTemporaryFile.new() → NamedTemporaryFile
NamedTemporaryFile.new(suffix) → NamedTemporaryFile
NamedTemporaryFile.new(suffix, prefix) → NamedTemporaryFile
NamedTemporaryFile.new(suffix, prefix, dir) → NamedTemporaryFile
NamedTemporaryFile.new(suffix, prefix, dir, delete) → NamedTemporaryFile
  • suffix (String) - Suffix for the filename (default: "")
  • prefix (String) - Prefix for the filename (default: "tmp")
  • dir (String|Null) - Directory for the file (default: null, uses TempFile.gettempdir())
  • delete (Bool) - Whether to delete the file on close (default: true)

Properties

.name → String

The full path of the temporary file.

.path → Path

A Path object for the temporary file.

.delete → Bool

Whether the file will be deleted on close. Can be set.

.closed → Bool

Whether the file has been closed.

Methods

.write(content)

Writes string content to the file, replacing any existing content.

.read() → String

Reads and returns the file content as a string.

.close()

Closes the file. If delete is true, the file is deleted from disk.

.use(fn) → *

Executes fn with the file as argument and guarantees cleanup afterwards, even if an error occurs. Equivalent to Python's with statement.

NamedTemporaryFile.new(".txt").use {|f|
    f.write("temporary data")
    System.print(f.read())
}
// File is automatically deleted here

TemporaryDirectory Class

TemporaryDirectory

A temporary directory with recursive cleanup support

Constructors

TemporaryDirectory.new() → TemporaryDirectory
TemporaryDirectory.new(suffix) → TemporaryDirectory
TemporaryDirectory.new(suffix, prefix) → TemporaryDirectory
TemporaryDirectory.new(suffix, prefix, dir) → TemporaryDirectory
TemporaryDirectory.new(suffix, prefix, dir, delete) → TemporaryDirectory
  • suffix (String) - Suffix for the directory name (default: "")
  • prefix (String) - Prefix for the directory name (default: "tmp")
  • dir (String|Null) - Parent directory (default: null, uses TempFile.gettempdir())
  • delete (Bool) - Whether to delete the directory on cleanup (default: true)

Properties

.name → String

The full path of the temporary directory.

.path → Path

A Path object for the temporary directory.

.delete → Bool

Whether the directory will be deleted on cleanup. Can be set.

.closed → Bool

Whether the directory has been cleaned up.

Methods

.cleanup()

Removes the directory and all its contents recursively (using Path.rmtree()). If delete is false, the directory is not removed.

.use(fn) → *

Executes fn with the directory as argument and guarantees cleanup afterwards, even if an error occurs.

TemporaryDirectory.new().use {|d|
    var path = d.name + "/data.txt"
    // Create files, do work...
}
// Directory and all contents automatically removed

Examples

Processing Files Safely

import "tempfile" for NamedTemporaryFile
import "pathlib" for Path

NamedTemporaryFile.new(".csv", "report_").use {|tmp|
    tmp.write("name,value\nalice,42\nbob,17")
    var data = tmp.read()
    System.print("Processing %(tmp.name)")
    System.print(data)
}

Working Directory for Build Output

import "tempfile" for TemporaryDirectory
import "pathlib" for Path

TemporaryDirectory.new("_build").use {|build|
    var src = Path.new(build.name) / "output.txt"
    src.writeText("Build artifact")
    System.print("Build dir: %(build.name)")
}
// Build directory cleaned up automatically

Persistent Temporary Files

import "tempfile" for NamedTemporaryFile

var tmp = NamedTemporaryFile.new(".log", "app_", null, false)
tmp.write("Application started")
tmp.close()
// File persists at tmp.name after close
System.print("Log file: %(tmp.name)")
Note

File creation via mkstemp uses exclusive creation flags (O_EXCL) for atomic file creation, preventing race conditions between checking for existence and creating the file. Random suffixes are generated using Crypto.randomBytes for cryptographic quality randomness.