What Is A Stream Position Pointer In Python Handling Of Files

Pavol Kutaj
1 min readFeb 5, 2022

--

The aim of this page📝 is to explain the concept of stream position pointer essential for file handling with Python.

1. NOTES

  • stream position is similar to a pointer
  • you get it by tell() function
  • you set it by seek() function
  • for binary files, it is a byte offset from the start of the file.
  • in text files, it is just an offset meaningful to the file object — the docs call it an opaque number
  • it has no defined physical meaning in terms of how the file is stored on disk
  • but in both modes (binary/test): it is an offset from the start
  • the start, therefore, has the stream position 0
  • the stream position is where the next operation will start from — not where the current one finished
  • after you’ve written a string to the file, the stream position is usually one offset value past the last char
  • when you’ve just opened a file, the offset is usually 0 or the end of the file, this depends on the mode file handling
  • it is 0 if you've opened it in r mode (read)
  • it is the end if you’ve opened it in a (append)
  • if at the end, it is +1 from the maximum value you could read from without getting EOF error
  • the two are equivalent for w and w+ modes since those truncate the file to zero bytes

2. LINKS

--

--

No responses yet