line number var like perl's $.?

Matthew Wilson mwilson at sarcastic-horse.com
Mon Nov 17 10:51:52 EST 2003


One thing I miss about perl was the builtin $. variable that gets
increased after each call to perl's file iterator object.  For example:

while ( my $line = <IN>) {
    print "$. $line";
}

or, more perlish:

while (<IN>) {
    print "$. $_";
}

Tracking line numbers is such a common thing to do when parsing files
that it makes sense for there to be a builtin for it.

Is there an equivalent construct in python?  Or are people doing
something like this:

linenum = 0
for line in open('blah.txt'):
    linenum += 1
    print linenum, ". ", line
 
Better ideas are welcomed.




More information about the Python-list mailing list