我需要为 Windows 上的某些文件获取 mime 类型,所以我已经安装了python-magic
(在 32 位 python 2.7.3 上)。
它取决于 unixmagic
库。作者指示从 gnuwin32 项目中
获取regex2.dll
和获取。所以我将文件保存到一个文件夹并将该文件夹添加到我的系统中。
现在当我执行方法时,我得到了丢失的文件异常: zlib1.dll
magic1.dll
PATH
magic
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
magic
magic.mgc
WindowsError: [Error 126]
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!
。
我缺少什么文件?
更新:
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>
只需要明确指定文件