1---2title: 'Python file magic'3date: '2007-05-08'4published_at: '2007-05-08T13:50:00.000+10:00'5tags: ['code', 'linux', 'programming', 'python']6author: 'Gavin Jackson'7excerpt: 'File magic is used on Linux and Unix systems as a binary "fingerprint" to tell what type a file actually is - this provides a much better result than MIME or filename extensions. The "magic" pattern i...'8updated_at: '2007-05-08T14:03:47.806+10:00'9legacy_url: 'http://www.gavinj.net/2007/05/python-file-magic.html'10---1112File magic is used on Linux and Unix systems as a binary "fingerprint" to tell what type a file actually is - this provides a much better result than MIME or filename extensions.1314The "magic" pattern is a text file definition that specifies header/footer and content binary patterns for file types on Ubuntu the file is located /usr/share/file/magic.1516Example of an entry in the magic file:1718```19#------------------------------------------------------------------------------2021# rtf: file(1) magic for Rich Text Format (RTF)22#2324# Duncan P. Simpson, D.P.Simpson@dcs.warwick.ac.uk25#260 string {\\rtf Rich Text Format data,27>5 byte x version %c,28>6 string \\ansi ANSI29>6 string \\mac Apple Macintosh30>6 string \\pc IBM PC, code page 43731>6 string \\pca IBM PS/2, code page 85032```3334We can interface with file magic using python by installing the python-magic package (apt-get install python-magic).3536```37import magic3839ms = magic.open(magic.MAGIC_NONE)40ms.load()41type = ms.file("/path/to/some/file")42print type4344f = file("/path/to/some/file", "r")45buffer = f.read(4096)46f.close()4748type = ms.buffer(buffer)49print type5051ms.close()52```535455