折腾:
后,突然想到,Python中估计会有MIME方面的库的:
估计给定文件名后缀,就可以得到MIME
搜:
python MIME lib
看起来很好用:
1 2 3 | >>> import mime >>> mime.MimeType.fromName( 'myfile.png' ) <MimeType: image / png> |
->
mime库看起来也还可以。
1 2 3 4 5 6 | # For MIME types >>> import magic >>> mime = magic.Magic(mime = True ) >>> mime.from_file( "testdata/test.pdf" ) 'application/pdf' >>> |
抽空去试试。
这样就不用自己手动整理出:
后缀名 对应的MIME type了。
先去安装:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | ➜ 英语资源 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 |
然后去试试效果:

结果代码:
1 2 3 4 | import mime fileMimeType = mime.MimeType.fromName(curAudioFullFilename) logging.info( "fileMimeType=%s" , fileMimeType) |
出错:
1 2 3 4 5 6 7 8 9 10 | 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' |
而写代码期间,也发现了:


结果:
1 | fileMimeType = mime.MIMETypes.load_from_file(curAudioFullFilename) |
错误:
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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 |
再去试试:
发现这个也是:
pip install mime
靠,两个mime都是一样的名字啊
算了,根据:
的“python-mime – A proper MIME type library for Python”
去安装:
python-mime
1 2 3 4 | ➜ 英语资源 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 |
找不到。
放弃。
继续用:
1 | fileMimeType = mime.Types.of(curAudioFullFilename)[0].content_type |
结果可以了:
1 | audio /mpeg |

【总结】
最后是去用:
的库:
1 | pip install mime |
后,用:
1 2 3 | import mime fileMimeType = mime.Types.of(curAudioFullFilename)[ 0 ].content_type |
即可从文件名:
‘。。。/音频/Lots of Hearts.mp3′
中提取出来:
‘audio/mpeg’
转载请注明:在路上 » 【已解决】python中从文件名后缀推断出MIME类型