guessing file type

Tim Chase python.list at tim.thechases.com
Fri Jul 17 08:36:29 EDT 2009


> Hello,  I need to determine programmatically a file type from its 
> content/extension (much like the "file" UNIX command line utility)
> 
> I searched for a suitable Python library module, with little luck. Do 
> you know something useful ?

Are you looking for something comprehensive?  Or are you just 
looking for particular file-types that your application can 
handle?  I'd start with the python mimetypes library[1] which 
does detection based on extensions (to which you can add your own 
mappings).  For sniffing by content, there are a wide variety of 
document types, I don't know of any pre-existing library.  The 
first couple bytes can often tell you something, but you'd have 
to go digging into the source for "file" to see what it does.

-tkc

[1]
http://docs.python.org/library/mimetypes.html






More information about the Python-list mailing list