obspy.core.util.base.NamedTemporaryFile

class NamedTemporaryFile(dir=None, suffix=u'.tmp', prefix=u'obspy-')[source]

Bases: io.BufferedIOBase

Weak replacement for the Python’s tempfile.TemporaryFile.

This class is a replacement for tempfile.NamedTemporaryFile() but will work also with Windows 7/Vista’s UAC.

Parameters:
  • dir (str) If specified, the file will be created in that directory, otherwise the default directory for temporary files is used.
  • suffix (str) The temporary file name will end with that suffix. Defaults to '.tmp'.

Example

>>> with NamedTemporaryFile() as tf:
...     _ = tf.write(b"test")
...     os.path.exists(tf.name)
True
>>> # when using the with statement, the file is deleted at the end:
>>> os.path.exists(tf.name)
False
>>> with NamedTemporaryFile() as tf:
...     filename = tf.name
...     with open(filename, 'wb') as fh:
...         _ = fh.write(b"just a test")
...     with open(filename, 'r') as fh:
...         print(fh.read())
just a test
>>> # when using the with statement, the file is deleted at the end:
>>> os.path.exists(tf.name)
False

Attributes

__abstractmethods__ frozenset() -> empty frozenset object
__doc__ unicode(string [, encoding[, errors]]) -> object
__module__ str(object) -> string
closed

Public Methods

read
seek
tell
write

Special Methods

__enter__
__exit__
__init__