1

I would like to create a Python class which contains a temporary file.

If I use the usual tempfile.TemporaryFile() with a context manager to create a member variable in the constructor, then the context manager will close/delete the temporary file when the constructor exits. This is no good because I want the file to exist for the lifetime of the class.

I see that I could create my own context managed class using __enter__ and __exit__ methods, does anyone have any examples of this? (Maybe I just need to add a line to delete the file to the example in the link?)

Or maybe there's a better way of doing this?

Daniel Scott
  • 7,418
  • 5
  • 39
  • 58
  • Do the answers to this [question](https://stackoverflow.com/questions/20912896/is-there-a-way-to-automatically-close-a-python-temporary-file-returned-by-mkstem) help at all? – quamrana Nov 24 '22 at 11:05
  • Does this answer your question? [Explaining Python's '\_\_enter\_\_' and '\_\_exit\_\_'](https://stackoverflow.com/questions/1984325/explaining-pythons-enter-and-exit) – Homer512 Nov 24 '22 at 11:12
  • @quamrana, that link does not mention classes – Daniel Scott Nov 24 '22 at 11:27
  • @Homer512, Not really. It matches the link in my post. I know how to create a context managed class, but I'm looking for an implementation which manages a temporary file. Do I just need to add a line to delete the file, as I said in my post? – Daniel Scott Nov 24 '22 at 11:28
  • The first answer to the linked question is a class managing a ```dbconn``` handle and the ```__exit__``` method calls ```self.dbconn.close()```. Guess what method you would call on a ```tempfile.TemporaryFile``` handle? – Homer512 Nov 24 '22 at 11:45
  • Does it have to mention classes? You are only wanting the temporary file to last while the constructor is running. – quamrana Nov 24 '22 at 12:47
  • Yes. I need to create a class which uses a temporary file as part of its implementation. So I need the file to last for the lifetime of the class. – Daniel Scott Nov 24 '22 at 12:50
  • So not: `"close/delete the temporary file when the constructor exits."`? – quamrana Nov 24 '22 at 12:51
  • No. I was saying that *if* I used a context managed temporary file in the constructor, then it would be deleted when the constructor exits. I want the temporary file to exist for the lifecycle of the whole class. – Daniel Scott Nov 24 '22 at 13:36
  • 1
    Ok, I see. Perhaps just keep the file in a member variable and rely on reference counting to close the file when the instance is garbage collected. – quamrana Nov 24 '22 at 13:48

1 Answers1

0

I came up with the following

class TemporaryFile:
  def __init__(self, *, data: str):
      self._data = data

  def __enter__(self):
      self._file = NamedTemporaryFile()
      self._file.write(data)
      self._file.flush()
      return self

  def __exit__(self, exc_type, exc_value, exc_tb):
      self._file.close()

  def sample_method():
      pass

Which is used like so:

with TemporaryFile(data=data) as file:
   file.sample_method()
Daniel Scott
  • 7,418
  • 5
  • 39
  • 58