Python "list index out of range" other rule works
<p>I have tried to make a script to read out a csv file and determine some information.</p>

<p>Now I receive an error: </p>

<pre><code>Traceback (most recent call last):
  File "/home/pi/vullijst/vullijst.py", line 26, in &lt;module&gt;
    startdate = datetime.datetime.strptime (row[0],"%d-%m-%Y")
IndexError: list index out of range
</code></pre>

<p>Part of Script:</p>

<pre><code>import csv
import datetime
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

#Variabelen
smtpserver = ''
smtplogin = ''
smtppassword = ''
sender = ''
csvfile = '/home/pi/vullijst/vullijst.csv'

#Inlezen CSV File
f = open(csvfile)
csv_f = csv.reader(f, delimiter=';')
today = datetime.datetime.now()

#Tijd bepalen en opstellen E-mail
for row in csv_f:

        startdate = datetime.datetime.strptime (row[0],"%d-%m-%Y")
        enddate = datetime.datetime.strptime (row[1],"%d-%m-%Y")
        if today &gt;= startdate and today &lt;= enddate:
                receiver = row[3]
</code></pre>

<p>The csv file has the following structure:</p>

<pre><code>1-10-2016;12-10-2016;Test 1;test0@email.com;06-123456789 
12-10-2016;13-10-2016;Test 2;test1@email.com;06-123456789 
13-10-2016;14-10-2016;Test 3;test2@email.com;06-123456789 
14-10-2016;15-10-2016;Test 4;test3@email.com;06-123456790 
15-10-2016;16-10-2016;Test 5;test4@email.com;06-123456791 
16-10-2016;17-10-2016;Test 6;test5@email.com;06-123456792 
17-10-2016;18-10-2016;Test 7;test6@email.com;06-123456793
</code></pre>

<p>If I comment out this rule then I don't receive the error on the rule below. Does somebody know what is wrong?</p>

<p>Your csv file appears to have an empty line at the end, after the last row with real data. So your script reads all the real lines and processes them, but it breaks when the last line is parsed into an empty list. The <code>row[0]</code> you're trying to parse into a date isn't valid in that situation.</p>

<p>To avoid this issue, put a check at the top of your loop that skips the rest of the loop body if <code>row</code> is empty.</p>

<pre><code>for row in csv_f:
    if not row: # skip empty rows
        continue
    startdate = datetime.datetime.strptime (row[0],"%d-%m-%Y")
    # ...
</code></pre>

python