How can I detect if a file is binary (non-text) in python?

Posted by grieve on Stack Overflow See other posts from Stack Overflow or by grieve
Published on 2009-05-22T16:09:50Z Indexed on 2010/06/09 1:22 UTC
Read the original article Hit count: 255

Filed under:
|
|

How can I tell if a file is binary (non-text) in python? I am searching through a large set of files in python, and keep getting matches in binary files. This makes the output look incredibly messy.

I know I could use grep -I, but I am doing more with the data than what grep allows for.

In the past I would have just searched for characters greater than 0x7f, but utf8 and the like make that impossible on modern systems. Ideally the solution would be fast, but any solution will do.

© Stack Overflow or respective owner

Related posts about python

Related posts about files