obspy.core.util.base.NamedTemporaryFile

class NamedTemporaryFile(dir=None, suffix='.tmp', prefix='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
__dict__
__doc__ str(object=’‘) -> str
__module__ str(object=’‘) -> str
closed

Public Methods

close
detach Disconnect this buffer from its underlying raw stream and return it.
fileno Returns underlying file descriptor if one exists.
flush Flush write buffers, if applicable.
isatty Return whether this is an ‘interactive’ stream.
read
read1 Read and return up to n bytes, with at most one read() call to the underlying raw stream.
readable Return whether object was opened for reading.
readinto
readline Read and return a line from the stream.
readlines Return a list of lines from the stream.
seek
seekable Return whether object supports random access.
tell
truncate Truncate file to size bytes.
writable Return whether object was opened for writing.
write
writelines

Private Methods

Warning

Private methods are mainly for internal/developer use and their API might change without notice.

_checkClosed
_checkReadable
_checkSeekable
_checkWritable

Special Methods

__dir__ default dir() implementation
__enter__
__exit__
__format__ default object formatter
__init__
__new__ Create and return a new object.
__reduce__ helper for pickle
__reduce_ex__ helper for pickle
__sizeof__ size of object in memory, in bytes
__subclasshook__ Abstract classes can override this to customize issubclass().