std/tempfiles

Source   Edit  

This module creates temporary files and directories.

Experimental API, subject to change.

Procs

proc createTempDir(prefix, suffix: string; dir = ""): string {.
    ...raises: [OSError, IOError],
    tags: [ReadEnvEffect, ReadIOEffect, WriteDirEffect, ReadDirEffect],
    forbids: [].}

Creates a new temporary directory in the directory dir.

This generates a dir name using genTempPath(prefix, suffix, dir), creates the directory and returns it, possibly after retrying to ensure it doesn't already exist.

If failing to create a temporary directory, OSError will be raised.

Note: It is the caller's responsibility to remove the directory when no longer needed.
Note: dir must exist (empty dir will resolve to getTempDir).

Example:

import std/os
doAssertRaises(OSError): discard createTempDir("", "", "nonexistent")
let dir = createTempDir("tmpprefix_", "_end")
# dir looks like: getTempDir() / "tmpprefix_YEl9VuVj_end"
assert dirExists(dir)
removeDir(dir)
Source   Edit  
proc createTempFile(prefix, suffix: string; dir = ""): tuple[cfile: File,
    path: string] {....raises: [OSError], tags: [ReadEnvEffect, ReadIOEffect],
                    forbids: [].}

Creates a new temporary file in the directory dir.

This generates a path name using genTempPath(prefix, suffix, dir) and returns a file handle to an open file and the path of that file, possibly after retrying to ensure it doesn't already exist.

If failing to create a temporary file, OSError will be raised.

Note: It is the caller's responsibility to close result.cfile and remove result.file when no longer needed.
Note: dir must exist (empty dir will resolve to getTempDir).

Example:

import std/os
doAssertRaises(OSError): discard createTempFile("", "", "nonexistent")
let (cfile, path) = createTempFile("tmpprefix_", "_end.tmp")
# path looks like: getTempDir() / "tmpprefix_FDCIRZA0_end.tmp"
cfile.write "foo"
cfile.setFilePos 0
assert readAll(cfile) == "foo"
close cfile
assert readFile(path) == "foo"
removeFile(path)
Source   Edit  
proc genTempPath(prefix, suffix: string; dir = ""): string {....raises: [],
    tags: [ReadEnvEffect, ReadIOEffect], forbids: [].}

Generates a path name in dir.

The path begins with prefix and ends with suffix.

Note: dir must exist (empty dir will resolve to getTempDir).
Source   Edit