I would like to understand how
Example using the file
fake file with some text in a few lines
Question 1: How does the readline() know which line of text to read, given that successive calls to readline() read the text line by line?
When you open a file in python, it creates a
file object. File objects act as file descriptors, which means at any one point in time, they point to a specific place in the file. When you first open the file, that pointer is at the beginning of the file. When you call
readline(), it moves the pointer forward to the character just after the next newline it reads.
tell() function of a
file object returns the location the file descriptor is currently pointing to.
with open('file.txt', 'r') as fd: print fd.tell() fd.readline() print fd.tell() # output: 0 10
Question 2: Is there a way to start reading a line of text from the middle of a text? How would this work with respect to the CPU?
First off, reading a file doesn't really have anything to do with the CPU. It has to do with the operating system and the file system. Both of those determine how files can be read and written to. Barebones explanation of files
Example, jumping to the 2nd line in the example file and reading until the end:
import mmap import contextlib with open('fake.txt', 'r') as fd: with contextlib.closing(mmap.mmap(fd.fileno(), 0, access=mmap.ACCESS_READ)) as mm: print mm[10:] # output: with some text in a few lines