obspy.core.util.base.NamedTemporaryFile

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

Bases: 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

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

readinto1

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

Write a list of lines to stream.

Private Methods

Warning

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

NamedTemporaryFile._checkClosed()
NamedTemporaryFile._checkReadable()
NamedTemporaryFile._checkSeekable()
NamedTemporaryFile._checkWritable()

Special Methods

NamedTemporaryFile.__del__()
NamedTemporaryFile.__delattr__(name, /)

Implement delattr(self, name).

NamedTemporaryFile.__dir__()

Default dir() implementation.

NamedTemporaryFile.__enter__()[source]
NamedTemporaryFile.__eq__(value, /)

Return self==value.

NamedTemporaryFile.__exit__(exc_type, exc_val, exc_tb)[source]
NamedTemporaryFile.__format__(format_spec, /)

Default object formatter.

NamedTemporaryFile.__ge__(value, /)

Return self>=value.

NamedTemporaryFile.__getattribute__(name, /)

Return getattr(self, name).

NamedTemporaryFile.__gt__(value, /)

Return self>value.

NamedTemporaryFile.__hash__()

Return hash(self).

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

This method is called when a class is subclassed.

The default implementation does nothing. It may be overridden to extend subclasses.

NamedTemporaryFile.__iter__()

Implement iter(self).

NamedTemporaryFile.__le__(value, /)

Return self<=value.

NamedTemporaryFile.__lt__(value, /)

Return self<value.

NamedTemporaryFile.__ne__(value, /)

Return self!=value.

NamedTemporaryFile.__new__(**kwargs)
NamedTemporaryFile.__next__()

Implement next(self).

NamedTemporaryFile.__reduce__()

Helper for pickle.

NamedTemporaryFile.__reduce_ex__(protocol, /)

Helper for pickle.

NamedTemporaryFile.__repr__()

Return repr(self).

NamedTemporaryFile.__setattr__(name, value, /)

Implement setattr(self, name, value).

NamedTemporaryFile.__sizeof__()

Size of object in memory, in bytes.

NamedTemporaryFile.__str__()

Return str(self).

NamedTemporaryFile.__subclasshook__()

Abstract classes can override this to customize issubclass().

This is invoked early on by abc.ABCMeta.__subclasscheck__(). It should return True, False or NotImplemented. If it returns NotImplemented, the normal algorithm is used. Otherwise, it overrides the normal algorithm (and the outcome is cached).