Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)

I need to decide MIME-types from documents without postfix in python3 and I considered python-wizardry as a fitting arrangement therefor. Shockingly, it doesn't fill in as portrayed in this link

>>> 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'

I was astounded, that this didn't work all things considered: 

>>> 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 was unable to discover any data about how to utilize the class Magic anyplace, so I continued doing experimentation, until I sorted out, that it acknowledges examples of LP_magic_set just for ms. Some of them are returned by the module's techniques magic.magic_set() and magic_t(). So I attempted to instantiate Magic with both of them. At the point when I at that point call the record() strategy from the occasion, it will consistently restore a vacant outcome and the errlvl() technique reveals to me blunder no. 22. So how would I utilize magic at any rate? 

1 Answer

0 votes
by (26.4k points)

I think that you are befuddling various executions of python-magic

You seem to have introduced python-magic-5.19.1, nonetheless, you reference initially the documentation for python-magic-0.4.6, and also filemagic-1.6. I believe that you are in an ideal situation utilizing python-magic-0.4.6 as it is promptly accessible at PYPI and handily introduced by means of pip into virtualenv conditions. 

Documentation for python-magic-5.19.1 is difficult to find, yet I figured out how to get it to function this way:

>>> import magic

>>> m=magic.open(magic.MAGIC_NONE)

>>> m.load()

0

>>> m.file('/etc/passwd')

'ASCII text'

>>> m.file('/usr/share/cups/data/default.pdf')

'PDF document, version 1.5'

 

you can likewise get distinctive magic portrayals, for example MIME type: 

>>> m=magic.open(magic.MAGIC_MIME)

>>> m.load()

0

>>> m.file('/etc/passwd')

'text/plain; charset=us-ascii'

>>> m.file('/usr/share/cups/data/default.pdf')

'application/pdf; charset=binary'

or on the other hand for later forms of python-magic-5.30

Want to know more details about python? Come and join:- Python course

Browse Categories

...