lsst.utils.tests.getTempFilePath(ext: str, expectOutput: bool = True) → Iterator[str]

Return a path suitable for a temporary file and try to delete the file on success

If the with block completes successfully then the file is deleted, if possible; failure results in a printed warning. If a file is remains when it should not, a RuntimeError exception is raised. This exception is also raised if a file is not present on context manager exit when one is expected to exist. If the block exits with an exception the file if left on disk so it can be examined. The file name has a random component such that nested context managers can be used with the same file suffix.

ext : str

File name extension, e.g. .fits.

expectOutput : bool, optional

If True, a file should be created within the context manager. If False, a file should not be present when the context manager exits.


Path for a temporary file. The path is a combination of the caller’s file path and the name of the top-level function


# file tests/
import unittest
import lsst.utils.tests
class FooTestCase(unittest.TestCase):
    def testBasics(self):

    def runTest(self):
        with lsst.utils.tests.getTempFilePath(".fits") as tmpFile:
            # if tests/.tests exists then
            # tmpFile = "tests/.tests/testFoo_testBasics.fits"
            # otherwise tmpFile = "testFoo_testBasics.fits"
            # at the end of this "with" block the path tmpFile will be
            # deleted, but only if the file exists and the "with"
            # block terminated normally (rather than with an exception)