Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find line number of cursor in Python?

Tags:

python

file-io

I am reading a file in Python. After I read a bunch of lines, is there a convenient function that I can use to get the current line number in the file that the cursor is on?

I am trying to refrain from using a counter since I have multiple functions that read the same file and may move the cursor all over the place.

Thanks

like image 890
David Avatar asked May 20 '11 02:05

David


1 Answers

You can go line-by-line with fileinput:

import fileinput
for line in fileinput.input(encoding="utf-8"):
   process(line)
like image 84
S.Lott Avatar answered Oct 04 '22 05:10

S.Lott