Temporary file handling

AUTHORS:

  • Volker Braun, Jeroen Demeyer (2012-10-18): move these functions here from sage/misc/misc.py and make them secure, see trac ticket #13579.
sage.misc.temporary_file.delete_tmpfiles()

Remove the directory SAGE_TMP.

TESTS:

This is automatically run when Sage exits, test this by running a separate session of Sage:

sage: from sage.tests.cmdline import test_executable
sage: child_SAGE_TMP, err, ret = test_executable(["sage", "-c", "print SAGE_TMP"])
sage: err, ret
('', 0)
sage: os.path.exists(child_SAGE_TMP)
False

The parent directory should exist:

sage: parent_SAGE_TMP = os.path.normpath(child_SAGE_TMP + '/..')
sage: os.path.isdir(parent_SAGE_TMP)
True
sage.misc.temporary_file.graphics_filename(ext='png')

Return the next available canonical filename for a plot/graphics file.

sage.misc.temporary_file.tmp_dir(name='dir_', ext='')

Create and return a temporary directory in $HOME/.sage/temp/hostname/pid/

The temporary directory is deleted automatically when Sage exits.

INPUT:

  • name – (default: "dir_") A prefix for the directory name.
  • ext – (default: "") A suffix for the directory name.

OUTPUT:

The absolute path of the temporary directory created, with a trailing slash (or whatever the path separator is on your OS).

EXAMPLES:

sage: d = tmp_dir('dir_testing_', '.extension')
sage: d   # random output
'/home/username/.sage/temp/hostname/7961/dir_testing_XgRu4p.extension/'
sage: os.chdir(d)
sage: _ = open('file_inside_d', 'w')

Temporary directories are unaccessible by other users:

sage: os.stat(d).st_mode & 0o077
0
sage.misc.temporary_file.tmp_filename(name='tmp_', ext='')

Create and return a temporary file in $HOME/.sage/temp/hostname/pid/

The temporary file is deleted automatically when Sage exits.

Warning

If you need a particular file extension always use tmp_filename(ext=".foo"), this will ensure that the file does not yet exist. If you were to use tmp_filename()+".foo", then you might overwrite an existing file!

INPUT:

  • name – (default: "tmp_") A prefix for the file name.
  • ext – (default: "") A suffix for the file name.

OUTPUT:

The absolute path of the temporary file created.

EXAMPLES:

sage: fn = tmp_filename('just_for_testing_', '.extension')
sage: fn  # random
'/home/username/.sage/temp/hostname/8044/just_for_testing_tVVHsn.extension'
sage: _ = open(fn, 'w')

Temporary files are unaccessible by other users:

sage: os.stat(fn).st_mode & 0o077
0

Previous topic

Miscellaneous functions

Next topic

Bitsets

This Page