最新消息:20210917 已从crifan.com换到crifan.org

【已解决】python中从文件名后缀推断出MIME类型

Python crifan 576浏览 0评论
折腾:
【已解决】常见文件的MIME的写法
后,突然想到,Python中估计会有MIME方面的库的:
估计给定文件名后缀,就可以得到MIME
搜:
python MIME lib
18.1.4. email.mime: Creating email and MIME objects from scratch — Python 2.7.14 documentation
jleclanche/python-mime: Python implementation of the shared-mime-info XDG spec
看起来很好用:
>>> import mime
>>> mime.MimeType.fromName('myfile.png')
<MimeType: image/png>
mime 0.1.0 : Python Package Index
->
liluo/mime: MIME Types, Python clone of halostatue/mime-types
mime库看起来也还可以。
How to find the mime type of a file in python? – Stack Overflow
# For MIME types
>>> import magic
>>> mime = magic.Magic(mime=True)
>>> mime.from_file("testdata/test.pdf")
'application/pdf'
>>>
抽空去试试。
这样就不用自己手动整理出:
后缀名 对应的MIME type了。
先去安装:
➜  英语资源 pip install mime
Collecting mime
  Downloading mime-0.1.0.tar.gz
Collecting future (from mime)
  Downloading future-0.16.0.tar.gz (824kB)
    100% |████████████████████████████████| 829kB 757kB/s
Building wheels for collected packages: mime, future
  Running setup.py bdist_wheel for mime ... done
  Stored in directory: /Users/crifan/Library/Caches/pip/wheels/ad/c5/51/fb656ef3fd92b30ea9821aa57e4f106232aeb7debfd7d6b11e
  Running setup.py bdist_wheel for future ... done
  Stored in directory: /Users/crifan/Library/Caches/pip/wheels/c2/50/7c/0d83b4baac4f63ff7a765bd16390d2ab43c93587fac9d6017a
Successfully built mime future
Installing collected packages: future, mime
Successfully installed future-0.16.0 mime-0.1.0
然后去试试效果:
结果代码:
import mime

fileMimeType = mime.MimeType.fromName(curAudioFullFilename)
logging.info("fileMimeType=%s", fileMimeType)
出错:
Traceback (most recent call last):
  File "/Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py", line 1668, in <module>
    main()
  File "/Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py", line 1662, in main
    globals = debugger.run(setup['file'], None, None, is_module)
  File "/Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py", line 1072, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "/Users/crifan/dev/dev_root/company/xxx/projects/mongodb/saveLocalData/pymongoTest.py", line 70, in <module>
    fileMimeType = mime.MimeType.fromName(curAudioFullFilename)
AttributeError: 'module' object has no attribute 'MimeType'
而写代码期间,也发现了:
结果:
fileMimeType = mime.MIMETypes.load_from_file(curAudioFullFilename)
错误:
2018/03/29 05:57:12 LINE 59   INFO    ottoTheCatFile=None
Traceback (most recent call last):
  File "/Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py", line 1668, in <module>
    main()
  File "/Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py", line 1662, in main
    globals = debugger.run(setup['file'], None, None, is_module)
  File "/Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py", line 1072, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "/Users/crifan/dev/dev_root/company/xxx/projects/mongodb/saveLocalData/pymongoTest.py", line 70, in <module>
    fileMimeType = mime.MIMETypes.load_from_file(curAudioFullFilename)
  File "/usr/local/lib/python2.7/site-packages/mime/mime_types.py", line 49, in load_from_file
    __parsing_error(type_file, index, line, RuntimeError)
NameError: global name '_MIMETypes__parsing_error' is not defined
再去试试:
liluo/mime: MIME Types, Python clone of halostatue/mime-types
发现这个也是:
pip install mime
靠,两个mime都是一样的名字啊
算了,根据:
https://github.com/jleclanche/python-mime
的“python-mime – A proper MIME type library for Python”
去安装:
python-mime
➜  英语资源 pip install python-mime
Collecting python-mime
  Could not find a version that satisfies the requirement python-mime (from versions: )
No matching distribution found for python-mime
找不到。
放弃。
继续用:
https://github.com/liluo/mime
fileMimeType = mime.Types.of(curAudioFullFilename)[0].content_type
结果可以了:
audio/mpeg
【总结】
最后是去用:
https://github.com/liluo/mime
的库:
pip install mime
后,用:
import mime

fileMimeType = mime.Types.of(curAudioFullFilename)[0].content_type
即可从文件名:
‘。。。/音频/Lots of Hearts.mp3′
中提取出来:
‘audio/mpeg’

转载请注明:在路上 » 【已解决】python中从文件名后缀推断出MIME类型

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
92 queries in 0.195 seconds, using 23.31MB memory