obspy.core.util.base.NamedTemporaryFile

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

Bases: object

Weak replacement for the Python’s tempfile.TemporaryFile.

This class is a replacment 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._fileobj  
...     tf.write("test")
...     os.path.exists(tf.name)
<open file '<fdopen>', mode 'w+b' at 0x...>
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("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

__dict__
__doc__ str(object=’‘) -> string
__module__ str(object=’‘) -> string
__weakref__ list of weak references to the object (if defined)

Special Methods

__enter__
__exit__
__getattr__
__init__

This Page