Windows上缺少`magic`库的文件 [英] Missing files for `magic` library on Windows

查看:186
本文介绍了Windows上缺少`magic`库的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要获取Windows上某些文件的mime类型,所以我已经安装了python-magic(在32位python 2.7.3上).
这取决于unix magic库.
作者指示从gnuwin32项目获取regex2.dllzlib1.dllmagic1.dll. 因此,我将文件保存到一个文件夹,并将该文件夹添加到我的系统PATH.
现在,当我执行magic方法时,我会丢失文件异常:

I need to get mime type for some files on windows, so i've installed python-magic (on 32-bit python 2.7.3).
It depends on unix magic library.
Author instructs to get regex2.dll, zlib1.dll and magic1.dll from gnuwin32 project. So i saved the files to a folder and added the folder to my system PATH.
Now when i execute magic methods, i get missing file exception:

import magic
print(magic.Magic())

Traceback (most recent call last):
File "C:/Users/Admin/PycharmProjects/lex/lex.py", line 367, in <module>
  test_magic()
File "C:/Users/Admin/PycharmProjects/lex/lex.py", line 364, in test_magic
  print(magic.Magic())
File "C:\Python27\lib\site-packages\python_magic-0.4.3-py2.7.egg\magic.py", line 52, in __init__
  magic_load(self.cookie, magic_file)
File "C:\Python27\lib\site-packages\python_magic-0.4.3-py2.7.egg\magic.py", line 188, in magic_load
  return _magic_load(cookie, coerce_filename(filename))
File "C:\Python27\lib\site-packages\python_magic-0.4.3-py2.7.egg\magic.py", line 139, in errorcheck
  raise MagicException(err)
magic.MagicException: could not find any magic files!

DLL位于PATH中,我尝试调试并且magic1.dll正确定位,但是库中的某个地方引发异常.
gnuwin32包中,我找到了magicmagic.mgc.我将它们放置在同一文件夹中,并在上添加了WindowsError: [Error 126]

DLLs are in the PATH, i tried debugging and magic1.dll is located correctly, but somewhere inside library throws an exception.
Inside the gnuwin32 package i've found magic and magic.mgc. I placed them to the same folder, and got WindowsError: [Error 126] on

libmagic = None  
# Let's try to find magic or magic1  
dll = ctypes.util.find_library('magic') or ctypes.util.find_library('magic1')  

# This is necessary because find_library returns None if it doesn't find the library
if dll:
    libmagic = ctypes.CDLL(dll)

这显然是因为python尝试将magic文件作为dll(纯文本)打开.将.dll添加到代码中的文件名后,我得到相同的magic.MagicException: could not find any magic files!.
我缺少什么文件?

This obviously happens because python tries to open magic file as dll, which is plain text. After adding .dll to filenames in the code i get the same magic.MagicException: could not find any magic files!.
What files am i missing?

更新:

C:\Users\Admin>file C:\123.zip -m magic
file: could not find any magic files!

C:\Users\Admin>file C:\123.zip -m "C:\@DEV\@LIB\@Magic\GetGnuWin32\bin\magic"
C:\123.zip; ASCII text, with no line terminators

C:\Users\Admin>cd C:\@DEV\@LIB\@Magic\GetGnuWin32\bin

C:\@DEV\@LIB\@Magic\GetGnuWin32\bin>file C:\123.zip -m magic
C:\123.zip; ASCII text, with no line terminators

更新2(已解决):

print(magic.Magic())
magic.MagicException: could not find any magic files!

print(magic.Magic(magic_file = 'magic'))
<magic.Magic instance at 0x02A5E198>

只需要明确指定文件

推荐答案

magic文件的路径必须显式传递给构造函数.

Path to magic file has to be explicitly passed to the constructor.

magic_object = magic.Magic(magic_file = 'path_to_magic_files/magic'))

这篇关于Windows上缺少`magic`库的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆