Python file magic Source

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---
11
12File 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.
13
14The "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.
15
16Example of an entry in the magic file:
17
18```
19#------------------------------------------------------------------------------
20
21# rtf:  file(1) magic for Rich Text Format (RTF)
22#
23
24# Duncan P. Simpson, D.P.Simpson@dcs.warwick.ac.uk
25#
260   string      {\\rtf      Rich Text Format data,
27>5  byte        x       version %c,
28>6  string      \\ansi      ANSI
29>6  string      \\mac       Apple Macintosh
30>6  string      \\pc        IBM PC, code page 437
31>6  string      \\pca       IBM PS/2, code page 850
32```
33
34We can interface with file magic using python by installing the python-magic package (apt-get install python-magic).
35
36```
37import magic
38
39ms = magic.open(magic.MAGIC_NONE)
40ms.load()
41type =  ms.file("/path/to/some/file")
42print type
43
44f = file("/path/to/some/file", "r")
45buffer = f.read(4096)
46f.close()
47
48type = ms.buffer(buffer)
49print type
50
51ms.close()
52```
53
54
55