0

I have a series of textfiles. They all end with a float, without a preceding whitespace ...foo123.456. The float has an unbounded number of digits.

the files are large so I would like to avoid reading them completely in memory. They have also different sizes.

How to avoid readgin the entire file?

00__00__00
  • 4,834
  • 9
  • 41
  • 89

1 Answers1

2

Just read the last few bytes and use a regular expression in order to extract the float number.

Untested:

import re

with open('/path/to/file.txt') as input_file:
    input_file.seek(-100, 2)
    last_100_bytes = input_file.read()
    match = re.search(r'\D(\d+\.\d+)$', last_100_bytes)
    if match:
        print('The float is {}'.format(match.group(0)))
    else:
        print('no float found at the end of the file')
Paulo Scardine
  • 73,447
  • 11
  • 124
  • 153
  • I think this is the answer after all...the over end of reading 100 bytes is insignificant as suggested by @Paulo Scardine – 00__00__00 Oct 19 '17 at 20:48