title: Detect last line (check EOF) of each input file
body: <p>I want to process some text files and detect last line of each one to do some operations at that point. In this simplified example, print file name, number of total lines and the odd ones. Assuming following content of files:</p>
  
  <pre><code>==&gt; file1 &lt;==
  one
  two
  three
  
  ==&gt; file2 &lt;==
  monday
  tuesday
  wednesday
  thursday
  
  ==&gt; file3 &lt;==
  red
  green
  blue
  </code></pre>
  
  <p>This is how I would implement it in <code>perl</code>:</p>
  
  <pre><code>#!/usr/bin/env perl
  
  use strict;
  use warnings;
  
  my (@odd);
  
  while ( &lt;&gt; ) {
          print $_;
          if ( $. &amp; 1 ) {
                  chomp;
                  push @odd, $_;
          }
          if ( eof ) {
                  print $ARGV, ' -- ', $., "\n";
                  print 'Odd lines =&gt; ', join( ':', @odd ), "\n";
                  undef @odd;
                  close ARGV;
          }
  }
  </code></pre>
  
  <p>For those who are not used to it, <code>&lt;&gt;</code> is similar to <code>fileinput.input()</code>and <code>ARGV</code> is the filehandle that I close explicitly to reset the line number counter (<code>$.</code>). I hope it's easy to understand. The key part is the check of <code>eof</code>.</p>
  
  <p>But this is how I try to do it with <code>python</code>:</p>
  
  <pre><code>#!/usr/bin/env python3
  
  from fileinput import *
  
  fname = ''
  lnumber = 0
  odd = []
  try:
      for line in input():   
          if filelineno() == 1 and fname:
              print('{0} -- {1}'.format(fname, lnumber))
              print('Odd lines =&gt; {0}'.format(':'.join(odd)))
              odd = []
              fname = ''
              lnumber = 0
          lnumber += 1
          print('{0}'.format(line), end='')
          if lnumber &amp; 1:
              odd.append(line.rstrip())
          if not fname:
              fname = filename()
          if not line:
              print('Is this end of line??')
  except OSError as e:
      print('Operation failed: {0}'.format(e.strerror))
  except EOFError as e:
      print('EOF found: {0}'.format(e.strerror))
  except StopIteration:
      print('StopIteration reached')
  finally:
      print('{0} -- {1}'.format(fname, lnumber))
      print('Odd lines =&gt; {0}'.format(':'.join(odd)))
      close()
  </code></pre>
  
  <p>Running it like:</p>
  
  <pre><code>python3 script.py file[123]
  </code></pre>
  
  <p>Both scripts yield same result: </p>
  
  <pre><code>one
  two
  three
  file1 -- 3
  Odd lines =&gt; one:three
  monday
  tuesday
  wednesday
  thursday
  file2 -- 4
  Odd lines =&gt; monday:wednesday
  red
  green
  blue
  file3 -- 3
  Odd lines =&gt; red:blue
  </code></pre>
  
  <p>It does what I want but not the way I want. None of <code>StopIteration</code>, <code>EOFError</code> or a check on an empty line detect last one. And the use of additional variables and <code>fileinput.filelineno()</code> to do post-processing of previous file seems weird for me, at least compared with <code>perl</code>. Did I miss something? How would you solve this kind of problem in a better way?</p>