How to detect end of file in Python after iterating the file object with "for" statement <p>I know to iterate a file line by line, I can use construct like this:</p> <pre><code>for line in file: do stuff </code></pre> <p>But if I also have a <strong>break</strong> statement somewhere inside the <strong>for</strong>, once I am out of the <strong>for</strong> block, how do I tell if it is the <strong>break</strong> that take me out of the <strong>for</strong> construct <em>OR</em> it is the because I hit the end of file already?</p> <p>I tried the suggestion from <a href="http://stackoverflow.com/questions/10140281/how-to-find-out-whether-a-file-is-at-its-eof">How to find out whether a file is at its `eof`?</a>:</p> <pre><code>f.tell() == os.fstat(f.fileno()).st_size </code></pre> <p>But that doesn't seem to work on my Windows machine. Basically, f.tell() always returns the size of the file.</p>