try something like this:
def is_binay_file(filepathname): textchars = bytearray([7,8,9,10,12,13,27]) + bytearray(range(0x20, 0x7f)) + bytearray(range(0x80, 0x100)) is_binary_string = lambda bytes: bool(bytes.translate(None, textchars)) if is_binary_string(open(filepathname, 'rb').read(1024)): return True else: return False
use the method as follows:
is_binay_file('<your file path name>')
This will return True if the file is of binary type and False if it has text - it is easy to convert it to reflect your needs, fx. make the is_text_file function - I will leave it to you
serup source share