How to know if a file is a text file

Chris Rebert clp2 at rebertia.com
Sun Nov 15 07:34:10 EST 2009


On Sun, Nov 15, 2009 at 4:06 AM, Nobody <nobody at nowhere.com> wrote:
> On Sat, 14 Nov 2009 17:02:29 +0100, Luca Fabbri wrote:
>
>> I'm looking for a way to be able to load a generic file from the
>> system and understand if he is plain text.
>> The mimetype module has some nice methods, but for example it's not
>> working for file without extension.
>>
>> Any suggestion?
>
> You could use the "file" command. It's normally installed by default on
> Unix systems, but you can get a Windows version from:

FWIW, IIRC the heuristic `file` uses to check whether a file is text
or not is whether it contains any null bytes; if it does, it
classifies it as binary (i.e. not text).

Cheers,
Chris
--
http://blog.rebertia.com



More information about the Python-list mailing list