Now, let's say you want to read from a certain range of characters within the file. For purposes of an example, let's say you want from character's 10-15. You could read in the whole file, and read out a certain amount, whittling it down. But this is a hack compared to what os can do for you. Using os.lseek() and os.read(), you can get the information you need with comparatively little overhead - saving resources and increasing speed. The basic syntax for each is as follows:
os.lseek(fd, pos, how)In the first call, fd is 'file descriptor', pos is 'position', and how is an integer of 0, 1, or 2. This indicates how to calculate the position using pos. 0 is relative to the beginning of the file, 1 is relative to the current position within the file, and 2 is relative to the end. In the second call, n is the number of bytes you want to read from the position within the file.
os.read(fd, n)
To get characters 10-15, we first move to the tenth point from the head of the file:
os.lseek(3, 10, 0)Then we can read the 10-16 characters:
os.read(3,6)Depending on the numbers you feed os, you may get any manner of character combinations. As Python sees HTML as simple text, you will get HTML tags and the like. If, however, you copy this article as text and save it, you will get something different:
'th Pyt'Keep in mind that we are telling Python to move within the file according to characters - not words. Therefore, it treats spaces in the same way as it treats letters or numbers.
