Possible Duplicate:
How to get line count cheaply in Python?
In my work i need to open a file and count no. of lines in that, i tried with this
Last_Line = len(open(File_Name).readlines())
It was working fine. Now i have a problem, actual no. of lines in the file is 453, but if i print Last_Line it is showing only 339. If i try
print linecache.getline(File_Name, 350)
it is displaying the contents of line no. 350.
I tried opening the file in all modes.
Whether its problem with file or with my logic?
Please help.
thank you
You have mixed line endings. Your IDE is treating them all as valid, while Python is not. Open the file with the universal newlines flag
"U"to have Python take them all as valid line endings.The documentation for
linecachedoes not appear to specify how it handles line endings. Empirically, it uses universal newlines: