confused about the readline() return in Python
<p>I am a beginner in python. However, I have some problems when I try to use the readline() method.</p>

<pre><code>f=raw_input("filename&gt; ")
a=open(f)
print a.read()
print a.readline()
print a.readline()
print a.readline()
</code></pre>

<p>and my txt file is </p>

<pre><code>aaaaaaaaa
bbbbbbbbb
ccccccccc
</code></pre>

<p>However, when I tried to run it on a Mac terminal, I got this:</p>

<pre><code>aaaaaaaaa
bbbbbbbbb
ccccccccc
</code></pre>

<p>It seems that  readline() is not working at all.
But when I disable print a.read(), the readline() gets back to work.</p>

<p>This confuses me a lot. Is there any solution where I can use read() and readline() at the same time? </p>

<p>You need to understand the concept of file pointers. When you read the file, it is fully consumed, and the pointer is at the end of the file. </p>

<blockquote>
  <p>It seems that the readline() is not working at all. </p>
</blockquote>

<p>It is working as expected. There are no lines to read. </p>

<blockquote>
  <p>when I disable print a.read(), the readline() gets back to work.</p>
</blockquote>

<p>Because the pointer is at the beginning of the file, and the lines can be read </p>

<blockquote>
  <p>Is there any solution that I can use read() and readline() at the same time?</p>
</blockquote>

<p>Sure. Flip the ordering of reading a few lines, then the remainder of the file, or seek the file pointer back to a position that you would like. </p>

<p>Also, don't forget to close the file when you are finished reading it </p>

python