Python File readlines()
method return all lines in the file, as a list where each line is an item in the list object.
As an example, here f.readlines()
returns a list.
f = open("file.txt", "r")
print(f.readlines())
How to implement an equivalent file.readlines()
equivalent using Python mmap
?
I have to read all lines as a list as opposed to reading a single line from a file.
This is what I have tried so far based on How to read lines from a mmapped file?.
lines = []
with open(path, "rt", encoding="utf-8") as f:
m = mmap.mmap(f.fileno(), 0, prot=mmap.PROT_READ)
while True:
line = m.readline()
lines.append(line)
if line == "":
break
print(line)
m.close()
However, this code iterates forever and is not working as expected.