Detecting Binary content in files

Steven D'Aprano steven at
Wed Apr 1 02:39:00 CEST 2009

On Tue, 31 Mar 2009 09:23:05 -0700, ritu wrote:

> Hi,
> I'm wondering if Python has a utility to detect binary content in files?

Define binary content.

> Or if anyone has any ideas on how that can be accomplished?

Step one: read the file.

Step two: does any of the data you have read match your definition of 
binary content? If so, then you have detected binary content.

Step three: there is no step three.

> I haven't
> been able to find any useful information to accomplish this (my other
> option is to fire off a perl script from within m python script that
> will tell me whether the file is binary), so any pointers will be
> appreciated.

Look at the perl script and see how it does it. Does it give false 
positives for Unicode text files?


More information about the Python-list mailing list