Determine file type (binary or text)
Trent Mick
trentm at ActiveState.com
Wed Aug 13 13:56:23 EDT 2003
[Sami Viitanen wrote]
> Hello,
>
> How can I check if a file is binary or text?
>
> There was some easy way but I forgot it..
Generally I define a text file as "it has no null bytes". I think this
is a pretty safe definition (I would be interested to hear practical
experience to the contrary). Assuming that, then:
def is_binary(filename):
"""Return true iff the given filename is binary.
Raises an EnvironmentError if the file does not exist or cannot be
accessed.
"""
fin = open(filename, 'rb')
try:
CHUNKSIZE = 1024
while 1:
chunk = fin.read(CHUNKSIZE)
if '\0' in chunk: # found null byte
return 1
if len(chunk) < CHUNKSIZE:
break # done
finally:
fin.close()
return 0
Cheers,
Trent
--
Trent Mick
TrentM at ActiveState.com
More information about the Python-list
mailing list