title: Elegant way to .seek on an open file
body: <p>I've come across this issue more than once, when you open a file in python using <code>with open</code> while reading it in small bytes, once you've read the bytes if on the next line you read more bytes it starts from where you've left off with the last call. For example:</p>
  <pre><code>def read_it(filename):
      with open(filename, &quot;rb&quot;) as f:
          print f.read(4)
          print f.read(4)
          print f.read(4)
          print f.read(4)
  
  
  read_it(&quot;test.rtf&quot;)
  #&lt;= {\rt
  #&lt;= f1\a
  #&lt;= nsi\
  #&lt;= ansi
  </code></pre>
  <p>The solution for this issue is to add <code>.seek(0)</code> after each read to start from the beginning:</p>
  <pre><code>def read_it(filename):
      with open(filename, &quot;rb&quot;) as f:
          print f.read(4)
          f.seek(0)
          print f.read(4)
          f.seek(0)
          print f.read(4)
          f.seek(0)
          print f.read(4)
  
  
  read_it(&quot;test.rtf&quot;)
  #&lt;= {\rt
  #&lt;= {\rt
  #&lt;= {\rt
  #&lt;= {\rt
  </code></pre>
  <p>However, as you see calling <code>.seek</code> multiple times is pretty ugly. I'm look for a more elegant way to seek back to 0 after each read. Is this possible?</p>
  <p>As mentioned this is python2, here's the same issue in python3:</p>
  <pre><code>Python 3.8.8 (tags/v3.8.8:024d805, Feb 19 2021, 13:18:16) [MSC v.1928 64 bit (AMD64)] on win32
  Type &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information.
  &gt;&gt;&gt; with open(&quot;test.rtf&quot;, &quot;rb&quot;) as f:
  ...     print(f.read(4))
  ...     print(f.read(4))
  ...     print(f.read(4))
  ...
  b'{\\rt'
  b'f1\\a'
  b'nsi\\'
  &gt;&gt;&gt;
  </code></pre>