title: Decorator to close open files
body: <p>I would like to write a decorator that closes all open files. For example:</p>
  
  <pre><code>@close_files
  def view(request):
      f = open('myfile.txt').read()
      return render('template.html')
  </code></pre>
  
  <p>Ignoring any threadsafe stuff, how could I write such a decorator to close out any open files after the function is returned? I'm not interested in writing a context manager, but something like this:</p>
  
  <pre><code>def close_files(func):
      @wraps(func)
      def wrapper_close_files(*args):
          return_value = func(*args, **kwargs)
          # close open files here?
          return return_value
      return wrapper_close_files
  </code></pre>
  
  <p>In your answer, please do not suggest something like this:</p>
  
  <pre><code>with open('myfile.txt') as _f: f = _f.read()
  </code></pre>
  
  <p>I'm asking about how to do a decorator to close files where we do not have direct access to the variable which references the file (handler).</p>