def readFromFile(filename): with open(filename, 'r+b') as file: res = file.read(100) return res 

How can you make it so that you can, by reading the first 100 characters, skip the next 100 and count the remaining characters? As far as I understand file.read() does not support offset ?

  • file.seek(file.tell() + 100); rest = file.read() file.seek(file.tell() + 100); rest = file.read() - should I also add a check for output beyond the end of the file? - MaxU
  • @ m9_psy you are confusing with the text mode. For rb, the units are bytes. - jfs

1 answer 1

How can you make it so that you can, by reading the first 100 characters, skip the next 100 and count the remaining characters?

 first100 = file.read(100) file.read(100) # skip rest = file.read() 

Alternatively, especially if more than 100 bytes need to be skipped:

 first100 = file.read(100) file.seek(100, os.SEEK_CUR) # skip rest = file.read()