I need to determine MIME-types from files without suffix in python3 and I thought of python-magic as a fitting solution therefor. Unfortunately it does not work as described here: https://github.com/ahupp/python-magic/blob/master/README.md
What happens is this:
>>> import magic
>>> magic.from_file("testdata/test.pdf")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'from_file'
So I had a look at the object, which provides me with the class Magic
for which I found documentation here:
http://filemagic.readthedocs.org/en/latest/guide.html
I was surprised, that this did not work either:
>>> with magic.Magic() as m:
... pass
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: __init__() missing 1 required positional argument: 'ms'
>>> m = magic.Magic()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: __init__() missing 1 required positional argument: 'ms'
>>>
I could not find any information about how to use the class Magic
anywhere, so I went on doing trial and error, until I figured out, that it accepts instances of LP_magic_set
only for ms
.
Some of them are returned by the module's methods
magic.magic_set()
and magic_t()
.
So I tried to instanciate Magic
with either of them.
When I then call the file()
method from the instance, it will always return an empty result and the errlvl()
method tells me error no. 22.
So how do I use magic anyway?
magic.py
file in the same directory as the one you launched the python shell from? The errors you got make it sound like you do (as I just got all your examples working). One way you can find out isimport inspect
theninspect.getfile(magic)
and see whether this is the expected file for themagic
module. – Halftone>>> import inspect >>> inspect.getfile(magic) '/usr/lib/python3.4/site-packages/magic.py'
– Economicspython-magic
. Yeah that's a completely different package to the one you looked at. Really though you could take a cursory glance at that file returned byinspect.getfile
and see that it probably completely differs from the one on GitHub. – Halftonehelp(obj)
to get some kind of help fromobj
via the builtin documentations. So in this casehelp(magic)
will also bring up any docstrings and available methods that clearly show that what you got on your system is not the same thing you got the documentation for. – Halftonepython-magic
package is NOT the same as the one you linked on github. They have the exact same name but are completely different. Heck, even the version number on pypi as linked (pypi.python.org/pypi/python-magic) is only at 0.4.6. Also, the distro version is the bindings to the magic library, which are NOT the pure native version. The only help you can get is from realizing your mistake here. – Halftone