3

I am wondering if it is possibly to read one byte at one given position at a time in python. I know about file.read(any number), but I want something that returns the byte I specify and ONLY the byte I specify. If I write: file.read(10), it reads the first 10 bytes of the file.

How do I say read byte 10 and only return byte 10? (this must work with all numbers greater than one) also, no classes please!

Zeugma
  • 31,231
  • 9
  • 69
  • 81
Josh Menzel
  • 2,300
  • 4
  • 22
  • 31
  • try `file.read(10)` followed by `file.read(1)` — will it be enough? – alf Jun 10 '12 at 22:28
  • 2
    Also, http://stackoverflow.com/questions/4999340/python-random-access-file – alf Jun 10 '12 at 22:29
  • @alf ...well -- that's asking for line-based as opposed to byte-based random access, though the mmap approach the top-rated answer suggests is worth keeping in mind as an alternative to simply seeking. – Charles Duffy Jun 10 '12 at 23:55

1 Answers1

7

Using seek and read, as show below should allow you to read one byte at a given position:

f.seek(10)
f.read(1)
octopusgrabbus
  • 10,555
  • 15
  • 68
  • 131
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452