标签: python-magic

运行32位Python 2.7版本的64位Windows 10的python-magic安装挑战

尽管遵循了该模块的自述文件,我仍然得到"未找到魔法文件".自述文件和"依赖关系"部分说:

在Windows上,将magic1.dll,regex2.dll和zlib1.dll从File for Windows项目提供的Binaries and Dependencies zip文件复制到PATH上.您将需要从[binary-zip]\share\misc中复制文件魔法,并将其位置传递给Magic(magic_file = ...)如果您使用的是64位版本的python,则需要64 -bit libmagic二进制文件,可以在这里找到:https://github.com/pidydx/libmagicwin64(注意:未经测试).

以下说明将有所帮助:

1)提供的Files for Windows链接有一个链接到"file-5.03-bin.zip"(标记为"Binaries"),链接到"file-5.03-dep.zip"(标记为"Dependencies"),以及一个链接在右下角的"file-4.26-dep.zip",它也被标记为"依赖文件".我假设最后一个是错误,应该指向5.03-dep,而不是另一个需要的第二个依赖项文件.正确?

在此输入图像描述

2)似乎有32位版本和64位版本的依赖项文件.我不知道哪个版本用于64位Windows而是32位Python.上面的说明说64位Python,但这说64位Windows. 我应该安装哪个?

3)上面说的把DLL放在PATH中; 64位指令说将它们放在C:\ Windows\System32中.是否重要,只要文件夹是PATH?(有人建议特别不要将它们放在\ Windows\System32帮助:Python-magic安装错误 - ImportError:找不到libmagic).

4)以上建议除了3个DLL之外,二进制文件和依赖项中唯一需要的其他文件是文件magic.所以我不需要文件magic.mgc,这也是二进制zip\share\misc?我也不需要这两个ZIP中的任何其他文件,比如文件file

5)上述建议得到文件magic出了二进制压缩,并把它的路径在一行代码它说:Magic(magic_file=...).我认为这实际上是magic.Magic(magic_file='C:\path\goes\here\magic')和我认为这可以在文件系统中的任何地方.

6)..我再次假设我指的是文件magic而不是文件magic.mgc.

这些假设是否正确?

windows dll python-2.7 libmagic python-magic

9
推荐指数
2
解决办法
2040
查看次数

在Windows上缺少`magic`库的文件

我需要为Windows上的某些文件获取mime类型,所以我安装了python-magic(在32位python 2.7.3上).
这取决于unix magic库.
作者指示得到regex2.dll,zlib1.dllmagic1.dll从项目的GnuWin32.所以我将文件保存到文件夹并将文件夹添加到我的系统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!
Run Code Online (Sandbox Code Playgroud)

DLL在PATH中,我尝试调试并且magic1.dll位置正确,但是库中的某处会引发异常.
里面gnuwin32包我找到了magic …

python windows gnuwin32 python-magic

8
推荐指数
2
解决办法
9047
查看次数

如何使用魔术来验证Django表单清洁方法中的文件类型?

我已经在Django中使用FileField编写了一个电子邮件表单类。我想通过检查其mimetype来检查上传文件的类型。随后,我想将文件类型限制为pdf,word和开放式Office文档。

为此,我已经安装了python-magic,并希望按照python-magic的规范检查文件类型,如下所示:

mime = magic.Magic(mime=True)
file_mime_type = mime.from_file('address/of/file.txt')
Run Code Online (Sandbox Code Playgroud)

但是,最近上传的文件在我的服务器上缺少地址。我还不知道任何类似于“ from_file_content”的mime对象的方法,该方法可以检查给定文件内容的mime类型。

使用魔术验证Django表单中上传文件的文件类型的有效方法是什么?

django django-forms django-file-upload python-magic

5
推荐指数
3
解决办法
2836
查看次数

Python-magic安装错误 - ImportError:找不到libmagic

我正在尝试为Windows安装python-magic,我已经按照https://github.com/ahupp/python-magic中的所有说明进行了几次重复该过程,但我仍然收到此错误:

ImportError: failed to find libmagic. Check your installation
Run Code Online (Sandbox Code Playgroud)

我在C:\ Windows\System32中有magic1.dll(以及文档指定的其他两个文件)所以我不确定是什么问题.我将不胜感激任何帮助或解决方法.

python-2.7 python-magic

5
推荐指数
3
解决办法
7904
查看次数

如何使用python-magic 5.19-1

我需要在python3中从没有后缀的文件中确定MIME类型,我认为python-magic是一个合适的解决方案.不幸的是它不能像这里描述的那样工作:https: //github.com/ahupp/python-magic/blob/master/README.md

这是怎么回事:

>>> 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'
Run Code Online (Sandbox Code Playgroud)

所以我看了一下这个对象,它为我提供了Magic我在这里找到文档的类:http: //filemagic.readthedocs.org/en/latest/guide.html

我很惊讶,这也不起作用:

>>> 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 …
Run Code Online (Sandbox Code Playgroud)

python python-magic

5
推荐指数
1
解决办法
2万
查看次数

导入错误:没有安装名为magic的模块,但安装了python-magic

我正在尝试编辑一些使用python-magic的代码,但我得到一个导入错误:没有名为magic的模块.在我环顾互联网并发现使用pip安装python-magic之前,我做了一些建议.我使用pip install python-magic安装了python-magic,并成功地安装了pip install libarchive -c.

当我尝试在python shell上进行导入时.我能成功如下; SC1

但是当我尝试运行使用此import语句的代码时,我得到一个导入错误,因为缺少魔术模块,如下所示; SC2

如果有人知道发生了什么.请帮忙.

python python-magic

5
推荐指数
1
解决办法
1万
查看次数

如果文件大小太小,为什么 python-magic 返回错误的 mime 类型?

如果文件大小低于 5000 字节 (InMemoryUploadedFile)。这段代码不起作用

mime_type = magic.from_buffer(file.read(), mime=True)
Run Code Online (Sandbox Code Playgroud)

它返回错误的 mime_type。例如,我有一个大小为 4074 字节的文件 cv.docx。它返回一个 mime_type:

'application/x-empty'
Run Code Online (Sandbox Code Playgroud)

代替

'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
Run Code Online (Sandbox Code Playgroud)

您能给我一些解决此案的建议吗?

python mime-types python-magic

4
推荐指数
1
解决办法
1652
查看次数

将python函数解析为decorator中的字符串

我正在尝试编写一个函数调试装饰器,它将会看到:

def foo(baz):
  bar = 1
  bar = 2
  return bar
Run Code Online (Sandbox Code Playgroud)

并将其包装到:

def foo(baz):
  bar = 1
  print 'bar: {}'.format(bar)
  bar = 2
  print 'bar: {}'.format(bar)
  return bar
Run Code Online (Sandbox Code Playgroud)

我需要使用函数作为文本,抓住"\ w +(?=\s*[=])",但不知道如何访问它.我有一个装饰我从一个有效的博客修改,但我只是尝试将其更改为:

class decorator_string_check(object):

   def __init__(self, func):
        self.func = func
        wraps(func)(self)

   def __call__(self, *args, **kwargs):
        print dir(self.func)
        print dir(self.func.__code__)
        print self.func.__code__.__str__()
        ret = self.func(*args, **kwargs)
        return ret

@decorator_string_check
def fake(x):
    y = 6
    y = x
    return y

y = fake(9)
Run Code Online (Sandbox Code Playgroud)

并且我正在获得有价值的东西,即:

['__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__', '__doc__', '__format__', …
Run Code Online (Sandbox Code Playgroud)

python decorator python-decorators python-magic

2
推荐指数
1
解决办法
478
查看次数

确定内存中的图像

我想确定我拥有(下载)的缓冲区是否是图像文件,而不将其保存到磁盘。我查了一下,发现:

  1. imghdr 只能对文件执行此操作。
  2. python-magic 可以给我文件类型,但我需要将它映射到图像类型。

有没有更好的办法?

python identification python-imaging-library python-magic imghdr

2
推荐指数
1
解决办法
578
查看次数

尝试在 Google Colab 中安装/使用 Python-Magic 时出错

我正在尝试在 Colab 中安装 python-magic。这是该库的 Github https://github.com/ahupp/python-magic#dependencies

当我尝试时:

!pip install python-magic
import magic
Run Code Online (Sandbox Code Playgroud)

这导致

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-44-1461204615fc> in <module>()
      1 get_ipython().system('pip install python-magic')
----> 2 import magic

/usr/local/lib/python3.6/dist-packages/magic.py in <module>()
    179 if not libmagic or not libmagic._name:
    180     # It is better to raise an ImportError since we are importing magic module
--> 181     raise ImportError('failed to find libmagic.  Check your installation')
    182 
    183 magic_t = ctypes.c_void_p

ImportError: failed to find libmagic.  Check your installation

--------------------------------------------------------------------------- …
Run Code Online (Sandbox Code Playgroud)

python python-magic google-colaboratory

1
推荐指数
1
解决办法
9488
查看次数

为什么模块 python-magic 不可用?

我正在使用s3cmd从 Mac 同步到 S3 存储桶,但收到以下警告:

WARNING: Module python-magic is not available. Guessing MIME types based on file extensions.
Run Code Online (Sandbox Code Playgroud)

pip list显示python-magic实际上已安装。

Package      Version
------------ -------
pip          19.3.1 
python-magic 0.4.15 
setuptools   42.0.2 
wheel        0.33.6 
Run Code Online (Sandbox Code Playgroud)

python --version展示

Python 3.7.5
Run Code Online (Sandbox Code Playgroud)

pip --version展示

pip 19.3.1 from /usr/local/Cellar/python/3.7.5/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pip (python 3.7)
Run Code Online (Sandbox Code Playgroud)

我该如何解决这个问题?

python pip s3cmd python-magic

1
推荐指数
1
解决办法
6485
查看次数