Read this blog post in Brazilian Portuguese.I was looking for a simple and fast Python library to implement proper filetype detection and encodingdetection into my rows library and found thatthere are many libraries available on Python PackageIndex which claim to do it. None of them attracted me because one ofthe following reasons:Do not have a pythonic implementation,Is not available on Debian to install as a package (it's important so peoplecan install rows and its dependencies using pip or apt-get),Is not maintained anymore, orHave some missing feature.None seemed to be the de-facto way to do it in Python (I think pythonistas doit in many ways). Many pythonistas use the chardet library but itsresults are wrong sometimes and it's pretty slow (specially if you need todetect during an API HTTP request, while the client is waiting).But there should be one -- and preferably only one -- obviousway to do it.So, I thought: why not use the file software, which is wellknown by all UNIX hackers, faster and most accurate than all the othersolutions I know? To my surprise there was not such a good Python binding forfile on PyPI (and calling it as a child process wasnot and option since it would add one more system-dependant package, notdetectable during a pip install if missing and would also turn this solutionless portable). Then, searching on its repository I found asimple Python wrapper, which was not available onPyPI at that time and was not that pythonic as Iexpected.The SolutionSince it's free/libre software, I've created an issue on file bugtracker to solve the problem and Christos Zoulas (thecurrent maintainer) asked for a patch, which Iimplemented, sent and was accepted. I'm pretty happy I cancontribute to a software I've been using since my earlier times on GNU/Linux(2003? 2004?). During this process I found that the first commit on thefree/libre file (which every GNU/Linux distribution and BSDflavor uses) implementation was done when I was less than 4 months old (!)-- and it's still maintained today.Now you can use the official file Python binding: thenew library is called file-magic and can be installed by running:pip install file-magicIt provides some methods and attributes but the most important are prettysimple and intuitive to use: they return a namedtuple with thedata you want! Let's take a tour through an example:>>> import magic>>> # You can pass the filename and it'll open the file for you:>>> filename_detected = magic.detect_from_filename('turicas.jpg')>>> print filename_detectedFileMagic(mime_type='image/jpeg', encoding='binary', name='JPEG image data, JFIF standard 1.02, aspect ratio, density 1x1, segment length 16, progressive, precision 8, 842x842, frames 3')>>> # It's a `namedtuple` so you can access the attributes directly:>>> print filename_detected.mime_typeimage/jpeg# If you have the file contents already, just use `detect_from_content`:>>> with open('data.html') as fobj:... data = fobj.read()>>> content_detected = magic.detect_from_content(data)>>> print content_detectedFileMagic(mime_type='text/html', encoding='utf-8', name='HTML document, UTF-8 Unicode text')>>> print content_detected.encodingutf-8There are still some things to be improved (like running tests in otherplatforms -- including Python 3) but it's pip-installable and usable now, so wecan benefit from it. Feel free to contribute. :)Hope you enjoy it! O EventoNo ultimo fim de semana de agosto tive o prazer de participar doUaiPython: o