Detecting Binary content in files
Matt Nordhoff
mnordhoff at mattnordhoff.com
Tue Mar 31 12:59:45 EDT 2009
ritu wrote:
> Hi,
>
> I'm wondering if Python has a utility to detect binary content in
> files? Or if anyone has any ideas on how that can be accomplished? 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.
>
> Thanks,
> Ritu
There isn't any perfect test. The usual heuristic is to check if there
are any NUL bytes in the file:
>>> '\0' in some_string
That can fail, of course. UTF-16-encoded text will have tons of NUL
bytes, and some binary files may not have any.
--
More information about the Python-list
mailing list