title: Finding open file objects
body: <p>How can I find a list of open file objects at a snapshot in time?</p>
  
  <p>Consider a few cases where files can be opened <em>without</em> being closed (either via <code>with</code> or <code>f.close()</code>):</p>
  
  <pre><code>import io
  import typing
  
  buf = io.StringIO()
  buf.write('memory here')  # still open
  
  f = open('/tmp/file.tmp', 'w')
  f.write('more memory')  # still open
  
  def g() -&gt; None:
      open('/tmp/file2.tmp', 'w')  # unclosed file
  g()
  
  
  def h() -&gt; typing.TextIO:
      return open('/tmp/file3.tmp', 'w')
  res = h()  # open (res.closed is False)
  </code></pre>
  
  <p>I am aware that the GC will eventually destroy the objects and close the open files on its own.  However, I am interested in manually finding which open files may be using system resources at any given time.</p>
  
  <p>One way it seems is to look in <code>__warningregistry__</code>; is this the defacto way to do such discovery, or is there a better route?</p>
  
  <pre><code>&gt;&gt;&gt; g()
  &gt;&gt;&gt; __warningregistry__
  {'version': 0, ("unclosed file &lt;_io.TextIOWrapper name='/tmp/file2.tmp' mode='w' encoding='UTF-8'&gt;",
   &lt;class 'ResourceWarning'&gt;, 2): True}
  </code></pre>