0

Suppose I had opened to read a text file under name fileOpened, and it has the following lines:
line1
line2
line3
line4

If I execute fileOpened.readline() once, the output will be: 'line1'.
If I execute fileOpened.readline() again, the output will be: 'line2'.

My question is:

Is there a method which would allow me to access the previous/last line I 'produced' from the readline() function from some other function, say arbitrary function lastline() (readline() position - one line) on fileOpened.lastline() would output me: 'line2'?

martineau
  • 119,623
  • 25
  • 170
  • 301
  • 2
    "s there a method which would allow me..." The technique is to remember the information yourself, by assigning it to a variable. It's possible to jump around in files, but this generally works on the *byte* level, and generally you'll still have to *remember* a position to jump back to, if you want to jump back to a previous position. All of this is caused by *how files work*, not by Python. – Karl Knechtel May 26 '22 at 23:31
  • Python does (indirectly) buffer some file contents in memory while reading, for efficiency reasons; but you don't get direct access to that buffer, and it would never be guaranteed to contain (all of) the "previous line" anyway, plus there wouldn't be any direct way to figure out where that line started. – Karl Knechtel May 26 '22 at 23:33

0 Answers0