Python使用filetype精确判断文件类型


Posted in Python onJuly 02, 2017

filetype.py

Small and dependency free Python package to infer file type and MIME type checking the  magic numbers signature of a file or buffer.

This is a Python port from filetype Go package. Works in Python  +3 .

一个小巧自由开放Python开发包,主要用来获得文件类型。包要求Python 3.+

功能特色

•简单友好的API
•支持宽范围文件类型
•提供文件扩展名和MIME类型判断
•文件的MIME类型扩展新增
•通过文件(图像、视频、音频…)简单分析
•可插拔:添加新的自定义类型的匹配
•快,即使处理大文件
•只需要前261个字节表示的最大文件头,这样你就可以通过一个单字节
•依赖自由(只是Python代码,没有C的扩展,没有libmagic绑定)
•跨平台文件识别

安装

pip install filetype

API

详情请查看 annotated API reference .

实例

简单的文件类型识别

import filetype
 
def main():
 kind = filetype.guess('tests/fixtures/sample.jpg')
 if kind is None:
  print('Cannot guess file type!')
  return
 
 print('File extension: %s' % kind.extension)
 print('File MIME type: %s' % kind.mime)
 
if __name__ == '__main__':
 main()

支持类型

图片

• jpg  ?  image/jpeg
• png  ?  image/png
• gif  ?  image/gif
• webp  ?  image/webp
• cr2  ?  image/x-canon-cr2
• tif  ?  image/tiff
• bmp  ?  image/bmp
• jxr  ?  image/vnd.ms-photo
• psd  ?  image/vnd.adobe.photoshop
• ico  ?  image/x-icon

视频

• mp4  ?  video/mp4
• m4v  ?  video/x-m4v
• mkv  ?  video/x-matroska
• webm  ?  video/webm
• mov  ?  video/quicktime
• avi  ?  video/x-msvideo
• wmv  ?  video/x-ms-wmv
• mpg  ?  video/mpeg
• flv  ?  video/x-flv

音频

• mid  ?  audio/midi
• mp3  ?  audio/mpeg
• m4a  ?  audio/m4a
• ogg  ?  audio/ogg
• flac  ?  audio/x-flac
• wav  ?  audio/x-wav
• amr  ?  audio/amr

资料库

• epub  ?  application/epub+zip
• zip  ?  application/zip
• tar  ?  application/x-tar
• rar  ?  application/x-rar-compressed
• gz  ?  application/gzip
• bz2  ?  application/x-bzip2
• 7z  ?  application/x-7z-compressed
• xz  ?  application/x-xz
• pdf  ?  application/pdf
• exe  ?  application/x-msdownload
• swf  ?  application/x-shockwave-flash
• rtf  ?  application/rtf
• eot  ?  application/octet-stream
• ps  ?  application/postscript
• sqlite  ?  application/x-sqlite3
• nes  ?  application/x-nintendo-nes-rom
• crx  ?  application/x-google-chrome-extension
• cab  ?  application/vnd.ms-cab-compressed
• deb  ?  application/x-deb
• ar  ?  application/x-unix-archive
• Z  ?  application/x-compress
• lz  ?  application/x-lzip

字体

• woff  ?  application/font-woff
• woff2  ?  application/font-woff
• ttf  ?  application/font-sfnt
• otf  ?  application/font-sfnt

基准测试

使用链接中的文件进行测试,你可以点击获得到它: real files .

Environment: OSX x64 i7 2.7 Ghz
------------------------------------------------------------------------------------------ benchmark: 7 tests ------------------------------------------------------------------------------------------
Name (time in ns)                       Min                     Max                   Mean                StdDev                 Median                   IQR            Outliers(*)  Rounds  Iterations
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
test_infer_image_from_bytes        357.6279 (1.0)       29,166.5395 (1.0)       1,642.3360 (1.0)        380.9934 (1.0)       1,509.9843 (1.0)        158.9457 (1.0)       9095;13752  102301           6
test_infer_audio_from_bytes        953.6743 (2.67)      96,082.6874 (3.29)     16,534.5880 (10.07)    3,002.1143 (7.88)     15,974.0448 (10.58)      953.6743 (6.00)       4514;6051   41528           1
test_infer_video_from_bytes     13,828.2776 (38.67)    272,989.2731 (9.36)     16,151.3144 (9.83)     3,361.2320 (8.82)     15,020.3705 (9.95)       953.6743 (6.00)       2522;2887   22193           1
test_infer_image_from_disk      15,974.0448 (44.67)    108,957.2906 (3.74)     18,621.0844 (11.34)    3,895.4441 (10.22)    17,166.1377 (11.37)    1,192.0929 (7.50)       1528;1804   10206           1
test_infer_video_from_disk      23,841.8579 (66.67)    229,120.2545 (7.86)     28,691.3476 (17.47)    6,242.9901 (16.39)    25,987.6251 (17.21)    4,053.1158 (25.50)      1987;1247   15651           1
test_infer_zip_from_disk        26,941.2994 (75.33)    230,073.9288 (7.89)     32,123.3861 (19.56)    7,524.4988 (19.75)    29,087.0667 (19.26)    4,768.3716 (30.00)      1349;1292   16132           1
test_infer_tar_from_disk        33,855.4382 (94.67)    164,031.9824 (5.62)     36,884.4401 (22.46)    4,489.4443 (11.78)    36,001.2054 (23.84)      953.6743 (6.00)       1036;1828   14666           1
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Python 相关文章推荐
Django中对数据查询结果进行排序的方法
Jul 17 Python
深入讲解Python中的迭代器和生成器
Oct 26 Python
python getopt详解及简单实例
Dec 30 Python
Python用sndhdr模块识别音频格式详解
Jan 11 Python
在Windows中设置Python环境变量的实例讲解
Apr 28 Python
python matplotlib如何给图中的点加标签
Nov 14 Python
PyCharm无法识别PyQt5的2种解决方法,ModuleNotFoundError: No module named 'pyqt5'
Feb 17 Python
Keras实现将两个模型连接到一起
May 23 Python
Python装饰器如何实现修复过程解析
Sep 05 Python
如何使用PyCharm引入需要使用的包的方法
Sep 22 Python
使用django自带的user做外键的方法
Nov 30 Python
python3定位并识别图片验证码实现自动登录功能
Jan 29 Python
使用python实现tcp自动重连
Jul 02 #Python
详解python3中socket套接字的编码问题解决
Jul 01 #Python
详解Python中for循环是如何工作的
Jun 30 #Python
python 连接sqlite及简单操作
Jun 30 #Python
利用Python破解斗地主残局详解
Jun 30 #Python
Python实现的文本编辑器功能示例
Jun 30 #Python
Python构建XML树结构的方法示例
Jun 30 #Python
You might like
解析thinkphp import 文件内容变量失效的问题
2013/06/20 PHP
destoon会员注册提示“数据校验失败(2)”解决方法
2014/06/21 PHP
PHP使用GIFEncoder类处理gif图片实例
2014/07/01 PHP
跟我学Laravel之快速入门
2014/10/15 PHP
php的debug相关函数用法示例
2016/07/11 PHP
PHP读取Excel内的图片(phpspreadsheet和PHPExcel扩展库)
2019/11/19 PHP
用javascript获取地址栏参数
2006/12/22 Javascript
JS实现日期加减的方法
2013/11/29 Javascript
jQuery事件之键盘事件(ctrl+Enter回车键提交表单等)
2014/05/11 Javascript
判断JS对象是否拥有某属性的方法推荐
2016/05/12 Javascript
为什么JavaScript没有块级作用域
2016/05/22 Javascript
bootstrap——bootstrapTable实现隐藏列的示例
2017/01/14 Javascript
Angular 2父子组件数据传递之局部变量获取子组件其他成员
2017/07/04 Javascript
彻底搞懂JavaScript中的apply和call方法(必看)
2017/09/18 Javascript
Angular6封装http请求的步骤详解
2018/08/13 Javascript
vue同个按钮控制展开和折叠同个事件操作
2020/07/29 Javascript
[05:49]2014DOTA2TI4正赛第二日综述 昔日冠军纷纷落马 VG LGD占尽先机
2014/07/20 DOTA
python遍历文件夹并删除特定格式文件的示例
2014/03/05 Python
在Python操作时间和日期之asctime()方法的使用
2015/05/22 Python
简述Python2与Python3的不同点
2018/01/21 Python
Python 中字符串拼接的多种方法
2018/07/30 Python
OpenCV 使用imread()函数读取图片的六种正确姿势
2020/07/09 Python
详解python使用金山词霸的翻译功能(调试工具断点的使用)
2021/01/07 Python
Banggood官网:面向全球客户的综合商城
2017/04/19 全球购物
Bodum官网:咖啡和茶壶、玻璃器皿、厨房电器等
2018/08/01 全球购物
Nº21官方在线商店:numeroventuno.com
2019/09/26 全球购物
ASICS印度官方网站:日本专业运动品牌
2020/06/20 全球购物
安全生产活动月方案
2014/03/09 职场文书
中餐厅经理岗位职责
2014/04/11 职场文书
共筑中国梦演讲稿
2014/04/23 职场文书
岗位说明书范文
2014/05/07 职场文书
MySQL 逻辑备份与恢复测试的相关总结
2021/05/14 MySQL
Spring Boot mybatis-config 和 log4j 输出sql 日志的方式
2021/07/26 Java/Android
详解redis在微服务领域的贡献
2021/10/16 Redis
用Python仅20行代码编写一个简单的端口扫描器
2022/04/08 Python
Python安装及建立虚拟环境的完整步骤
2022/06/25 Servers