title: How to loop until EOF in Python?
body: <p>I need to loop until I hit the end of a file-like object, but I'm not finding an "obvious way to do it", which makes me suspect I'm overlooking something, well, obvious.  :-)</p>
  
  <p>I have a stream (in this case, it's a StringIO object, but I'm curious about the general case as well) which stores an unknown number of records in "&lt;length>&lt;data>" format, e.g.:</p>
  
  <pre><code>data = StringIO("\x07\x00\x00\x00foobar\x00\x04\x00\x00\x00baz\x00")
  </code></pre>
  
  <p>Now, the only clear way I can imagine to read this is using (what I think of as) an initialized loop, which seems a little un-Pythonic:</p>
  
  <pre><code>len_name = data.read(4)
  
  while len_name != "":
      len_name = struct.unpack("&lt;I", len_name)[0]
      names.append(data.read(len_name))
  
      len_name = data.read(4)
  </code></pre>
  
  <p>In a C-like language, I'd just stick the <code>read(4)</code> in the <code>while</code>'s test clause, but of course that won't work for Python.  Any thoughts on a better way to accomplish this?</p>