1. Calculate the number of objects
The simplest way is to read the file into a large list and calculate the length of the list. if the file path is transmitted in the form of a parameter filepath, we can use only one line of code to meet our requirements:
Count = len (open (filepath, 'ru '). readlines ())
If a file is very large, the above method may be slow or even invalid. At this time, you can use a loop to process it:
Count =-1
For count, line in enumerate (open (thefilepath, 'ru ')):
Pass
Count + = 1
Another way to process large files is to calculate the number of linefeeds '\ n' (or strings containing' \ n', such as in windows ):
Count = 0
Thefile = open (thefilepath, 'rb ')
While True:
Buffer = thefile. read (8192*1024)
If not buffer:
Break
Count + = buffer. count ('\ n ')
Thefile. close ()
The 'rb' parameter is required. Otherwise, the code above will be very slow on windows.
Linecache is a function library that supports reading large files and row-based reading. Linecache reads the file into the cache in advance. If you access the file later, the file will not be read from the hard disk.
2. Read the content of a row of a file (the efficiency of a 1 GB file has been tested)
Import linecache
Count = linecache. getline (filename, linenum)
3. Use linecache To Read File Content (1 GB files have been tested, and the efficiency is acceptable)
Str = linecache. getlines (filename)
Str is a list, and each row is an element in the list.