how to go the start of file in python?
<p>I am new to python and I am learning some basic file reading stuff. I am trying to read a file and count the number of new lines and also print lines that start with 'From: ' .</p>

<p>This is the code I have for that:</p>

<pre><code>fhand = open('mbox.txt')

count = 0

for line in fhand:

      count = count + 1

print count

for line in fhand:

    if line.startswith('From: '):

          print line
</code></pre>

<p>I know that I can do this in one loop but I am trying to learn something here. As soon as the first loop is executed, 'line' is at the end of the file. So when it runs the second loop, it does not print anything. I tried putting in line = 0, it doesnt work. How to I got back to start of file?</p>

<p>Thank you for your help. </p>

<pre><code>file.seek(0)
</code></pre>

<p><a href="https://docs.python.org/3.5/tutorial/inputoutput.html" rel="nofollow">Seek()</a> takes an argument that goes back to that "byte" so 0 byte will go back to the start of the file. </p>

python