-2

I read a part of a file:

size_to_read = 999999
with open(file_name, "r") as f:
  read_part = f.read(size_to_read)

How do I know the size of a data (read_part) that actually was read in case if a size of a file is less than size_to_read?

Mataba
  • 97
  • 1
  • 10

2 Answers2

3

Simply check the length of the string with the built in len function:

size_to_read = 999999  
with open(file_name, "r") as f:  
  read_part = f.read(size_to_read)  
  if(not len(read_part) == size_to_read):
    eol_reached_unexpectedly()
DeltaBurnt
  • 48
  • 4
0

How about using f.tell() and comparing the result to size_to_read?

Kimvais
  • 38,306
  • 16
  • 108
  • 142