mktemp(::Function, ?)

mktemp(f::Function, [parent=tempdir()])

Apply the function f to the result of mktemp(parent) and remove the temporary file upon completion.


julia> (path, io) = mktemp()
("/tmp/jl_XYZ.tmp", IOStream(<file /tmp/jl_XYZ.tmp>))

The mktemp() function generates a unique temporary file path and returns a tuple containing the path and an open file object for that path. By default, the temporary file is created in the system's temporary directory.

Common examples of its use:

  1. Create a temporary file in the default temporary directory:

    julia> (path, io) = mktemp()
    ("/tmp/jl_XYZ.tmp", IOStream(<file /tmp/jl_XYZ.tmp>))

    This example generates a unique temporary file path in the default temporary directory and returns the path along with an open file object.

  2. Specify a custom parent directory for the temporary file:

    julia> (path, io) = mktemp(parent="/path/to/directory")
    ("/path/to/directory/jl_XYZ.tmp", IOStream(<file /path/to/directory/jl_XYZ.tmp>))

    Here, the parent argument is used to specify a custom directory where the temporary file should be created.

  3. Perform operations on the temporary file:

    julia> (path, io) = mktemp()
    ("/tmp/jl_XYZ.tmp", IOStream(<file /tmp/jl_XYZ.tmp>))
    julia> write(io, "Hello, Julia!")
    julia> close(io)

    This example demonstrates writing to and closing the open file object obtained from mktemp(). The file can be used for any operations like reading, writing, or appending data.

Remember, the temporary file created by mktemp() will be automatically deleted when the file object is closed or when the Julia session terminates.

See Also

abspath, basename, chmod, countlines, cp, ctime, dirname, download, evalfile, expanduser, fdio, filemode, filesize, functionloc, gperm, homedir, include_string, isabspath, isblockdev, ischardev, isdir, isdirpath, isexecutable, isfifo, isfile, islink, ismount, ispath, isreadable, issetgid, issetuid, issticky, iswritable, joinpath, less, lstat, mkdir, mkpath, mktemp, mktempdir, mtime, mv, normpath, operm, poll_fd, poll_file, readall, readcsv, readdir, readdlm, readlines, readlink, realpath, relpath, rm, splitdir, splitdrive, splitext, stat, symlink, tempdir, tempname, touch, truncate, uperm, watch_file, writecsv,

User Contributed Notes

Add a Note

The format of note supported is markdown, use triple backtick to start and end a code block.

*Required Field

Checking you are not a robot: