In Python, the modification to the file object can be done in two different ways. The first way is to modify the file from its physical storage location by making system calls such as open, read, write, and more. The second modification way is to modify directly in the RAM without system calls using Memory mapping, which allows us to map file data into memory.
The file is read at once and kept as a file object for modifications. The mmap
module in Python uses the mmap
method to map bytes of the file to memory and returns the file object. The returned object is then used to count the number of lines.
mmap
functionThe mmap
function needs different arguments and is defined as given below.
mm_object = mmap.mmap(file_obj.fileno(), length=0,access=mmap.ACCESS_READ,offset=0)
file_obj.fileno()
is the file descriptorlength=0
is the size of memory. It is defined 0
so that system automatically selects sufficient memory required for the file dataaccess=mmap.ACCESS_READ
defines to the system how the program is going to access the memory. Other options are ACCESS_WRITE
and ACCESS_COPY
offset=0
defines the system to map the file to the memory after the bytes defined by the offset.import mmapwith open('test.txt', "r+") as file_obj:mm_object = mmap.mmap(file_obj.fileno(), length=0,access=mmap.ACCESS_READ,offset=0)lines_count = 0while mm_object.readline():lines_count += 1print(lines_count)
mmap
module from Python libraries.mm_object
to access the mapped file.mm_object.readline()
until the end of the file. It increments the lines_count
variable to get total counts at the end.Free Resources