code: def istextfile(fname, blocksize=512):
      
      with open(fname, "rb") as fobj:
          block = fobj.read(blocksize)
  
      if not block:
          # An empty file is considered a valid text file
          return True
  
      if b"\x00" in block:
          # Files with null bytes are binary
          return False
  
      # Use translate's 'deletechars' argument to efficiently remove all
      # occurrences of TEXT_CHARS from the block
      nontext = block.translate(None, TEXT_CHARS)
      return float(len(nontext)) / len(block) <= 0.30
docs: Uses heuristics to guess whether the given file is text or binary,
          by reading a single block of bytes from the file.
          If more than 30% of the chars in the block are non-text, or there
          are NUL ('\x00') bytes in the block, assume this is a binary file.