IndexError in this code
<p>When I use this as the contents of the input file:</p>

<pre><code>1,3,5,7,9,11
</code></pre>

<p>I receive this error: </p>

<pre><code>IndexError: li
</code></pre>

<hr>

<pre><code>with open('fig.fig') as o:
    n = 6
    for i in range(1, 2*n, 2):
        print(o.readlines()[i].replace(' ', ''))
</code></pre>

<p>When opening and processing a file, you should always make sure that you don't attempt to read more lines than are actually in the file. Also, you are making repeated calls to <code>readlines</code> which is quite inefficient. Try something like:</p>

<pre><code>with open('fig.fig') as o:    
    lines = o.readlines()
    n=6
    for i in range(1, min(len(o), n*2), 2):
       print(lines[i].replace(' ', ''))
</code></pre>

<p>This will read odd numbered lines, up to n*2, or the maximum number of lines in the file, whichever is less. You should also check that the file exists (one way is to use <code>os.path.isfile('fig.fig')</code>, or wrap everything in a <a href="https://docs.python.org/3.6/reference/compound_stmts.html#try" rel="nofollow" title="try">try</a> block.</p>

python indexing file-access