Determine file type (binary or text)

Peter Hansen peter at engcorp.com
Wed Aug 13 14:24:53 EDT 2003


Trent Mick wrote:
> 
> [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). 

"Contains only printable characters" is probably a more useful definition
of text in many cases.  I can't say off the top of my head exactly when
either definition might be a problem....  wait, how about this one: in
CVS, if you don't have a file that is effectively line-oriented, human
readable information, you probably don't want to let it be treated as 
"text" and stored as diffs.  In that situation, "contains primarily 
printable characters organized in lines" is probably a more thorough,
though less deterministic, definition.

-Peter




More information about the Python-list mailing list