It's Possible To Determine How Many Lines Exist In File Without Per Line Iteration?
Solution 1:
In general it's not possible to do better than reading every character in the file and counting newline characters.
It may be possible if you know details about the internal structure of the file. For example, if the file is 1024kB long, and every line is 1kB in length, then you can deduce there are 1024 lines in the file.
Solution 2:
I'm not sure if Python has that function or not, highly doubt it, but it would essentially require reading the whole file. A newline is signified by the \n character (actually system dependent) so there is no way to know how many of those exist in a file without going through the whole file.
Solution 3:
You could use the readlines() file method and this is probably the easiest.
If you want to be different, you could use the read() member function to get the entire file and count CR, LF,CRLR LFCR character combinations using collections.Counter class. However, you will have to deal with the various ways of terminating lines. Something like:
import collections
f=open("myfile","rb")
d=f.read()
f.close()
c=collections.Counter(d)
lines1=c['\r\n']
lines2=c['\n\r']
lines3=c['\r']-lines1-lines2
lines4=c['\n']-lines1-lines2
nlines=lines3+lines4
Solution 4:
No, such information can only be retrieved by iterating over the whole file's content (or reading the whole file into memory. But unless you know for sure that the files will always be small better don't even think about doing this).
Even if you do not loop over the file contents, the functions you call do. For example, len(f.readlines())
will read the whole file into a list just to count the number of elements. That's horribly inefficient since you don't need to store the file contents at all.
Solution 5:
This gives the answer, but reads the whole file and stores the lines in a list
len(f.readlines())
Post a Comment for "It's Possible To Determine How Many Lines Exist In File Without Per Line Iteration?"