HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Moduletemporary-1.3Haskell2010

System.IO.Temp

Functions to create temporary files and directories.

Most functions come in two flavours: those that create files/directories under the system standard temporary directory and those that use the user-supplied directory.

The functions that create files/directories under the system standard temporary directory will return canonical absolute paths (see getCanonicalTemporaryDirectory). The functions use the user-supplied directory do not canonicalize the returned path.

The action inside withTempFile or withTempDirectory is allowed to remove the temporary file/directory if it needs to.

Templates and file names

The treatment of templates differs somewhat for files vs directories.

For files, the template has form name.ext, and a random number will be placed between between the name and the extension to yield a unique file name, e.g. name1804289383846930886.ext.

For directories, no extension is recognized. A random hexadecimal string (whose length depends on the system's word size) is appended to the end of the template. For instance, the directory template dir may result in a directory named dir-e4bd89e5d00acdee.

You shouldn't rely on the specific form of file or directory names generated by the library; it has changed in the past and may change in the future.

  • 13 values
  • Packagetemporary-1.3
  • Exports13
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceTemp.hs
valuewithTempFile
  1. :: (MonadIO m, MonadMask m)
  2. => FilePath

    Parent directory to create the file in

  3. -> String

    File name template

  4. -> (FilePath -> Handle -> m a)

    Callback that can use the file

  5. -> m a
#

Create, open, and use a temporary file in the given directory.

The temp file is deleted after use.

valuewithTempDirectory
  1. :: (MonadMask m, MonadIO m)
  2. => FilePath

    Parent directory to create the directory in

  3. -> String

    Directory name template

  4. -> (FilePath -> m a)

    Callback that can use the directory

  5. -> m a
#

Create and use a temporary directory inside the given directory.

The directory is deleted after use.

valuewriteTempFile
  1. :: FilePath

    Parent directory to create the file in

  2. -> String

    File name template

  3. -> String

    Data to store in the file

  4. -> IO FilePath

    Path to the (written and closed) file

#

Create a unique new file, write (text mode) a given data string to it, and close the handle again. The file will not be deleted automatically, and only the current user will have permission to access the file.

valueemptyTempFile
  1. :: FilePath

    Parent directory to create the file in

  2. -> String

    File name template

  3. -> IO FilePath

    Path to the (written and closed) file

#

Create a unique new empty file. (Equivalent to writeTempFile with empty data string.) This is useful if the actual content is provided by an external process.

Re-exports from System.IO

2 declarations
valueopenTempFile
  1. :: FilePath

    Directory in which to create the file

  2. -> String

    File name template. If the template is "foo.ext" then the created file will be "fooXXX.ext" where XXX is some random number. Note that this should not contain any path separator characters. On Windows, the template prefix may be truncated to 3 chars, e.g. "foobar.ext" will be "fooXXX.ext".

  3. -> IO (FilePath, Handle)
#

The function creates a temporary file in ReadWrite mode. The created file isn't deleted automatically, so you need to delete it manually.

The file is created with permissions such that only the current user can read/write it.

With some exceptions (see below), the file will be created securely in the sense that an attacker should not be able to cause openTempFile to overwrite another file on the filesystem using your credentials, by putting symbolic links (on Unix) in the place where the temporary file is to be created. On Unix the O_CREAT and O_EXCL flags are used to prevent this attack, but note that O_EXCL is sometimes not supported on NFS filesystems, so if you rely on this behaviour it is best to use local filesystems only.

Auxiliary functions

1 declaration

Return the absolute and canonical path to the system temporary directory.

Example4 expressions
setCurrentDirectory "/home/feuerbach/"setEnv "TMPDIR" "."getTemporaryDirectory"."getCanonicalTemporaryDirectory"/home/feuerbach"