DeaD_EyE

check palindrome in a file

Jul 9th, 2020
717
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import os
  2.  
  3.  
  4. def check_palindrome(file, chunk_size=1024 ** 1 * 512):
  5.     size = os.path.getsize(file)
  6.     left_position = 0
  7.     right_position = size - chunk_size
  8.     with open(file) as fd:
  9.         while not left_position >= right_position:
  10.             fd.seek(left_position)
  11.             left_chunk = fd.read(chunk_size)
  12.             fd.seek(right_position)
  13.             right_chunk = fd.read(chunk_size)[::-1]
  14.             if left_chunk != right_chunk:
  15.                 return False
  16.             left_position += chunk_size
  17.             right_position -= chunk_size
  18.     return True
RAW Paste Data