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>When you open a file you get a pointer to some place of the file (by default: the begining). Now whenever you run <code>.read()</code> or <code>.readline()</code> this pointer moves:</p>

<ol>
<li><code>.read()</code> reads until the end of the file and moves the pointer to the end (thus further calls to any reading gives nothing)</li>
<li><code>.readline()</code> reads until newline is seen and sets the pointer after it</li>
<li><code>.read(X)</code> reads X bytes and sets the pointer at <code>CURRENT_LOCATION + X</code> (or the end)</li>
</ol>

<p>If you wish you can manually move that pointer by issuing <code>a.seek(X)</code> call where <code>X</code> is a place in file (seen as an array of bytes). For example this  should give you the desired output:</p>

<pre><code>print a.read()
a.seek(0)
print a.readline()
print a.readline()
print a.readline()
</code></pre>

python