f.readline() in your function function get_line() eventually returns '', and then you are trying to extract the first element of an empty string: ''[0] This produces an IndexError. Sebrosa