99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁精彩閱讀Python使用filetype精確判斷文件類型
Python使用filetype精確判斷文件類型
2018-05-04
收藏

Python使用filetype精確判斷文件類型

判斷文件類型在開發(fā)中非常常見的需求,怎樣才能準(zhǔn)確的判斷文件類型呢?首先大家想到的是文件的后綴,但是非常遺憾的是這種方法是非常不靠譜的,因為文件的后綴是可以隨意更改的,而python中有個小插件可以實(shí)現(xiàn),下面我們就來詳細(xì)探討下

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開發(fā)包,主要用來獲得文件類型。包要求Python 3.+

功能特色

?簡單友好的API
?支持寬范圍文件類型
?提供文件擴(kuò)展名和MIME類型判斷
?文件的MIME類型擴(kuò)展新增
?通過文件(圖像、視頻、音頻…)簡單分析
?可插拔:添加新的自定義類型的匹配
?快,即使處理大文件
?只需要前261個字節(jié)表示的最大文件頭,這樣你就可以通過一個單字節(jié)
?依賴自由(只是Python代碼,沒有C的擴(kuò)展,沒有l(wèi)ibmagic綁定)
?跨平臺文件識別

安裝

pip install filetype

API

詳情請查看 annotated API reference .

實(shí)例

簡單的文件類型識別

importfiletype
  
defmain():
 kind=filetype.guess('tests/fixtures/sample.jpg')
 ifkindisNone:
  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

基準(zhǔn)測試

使用鏈接中的文件進(jìn)行測試,你可以點(diǎn)擊獲得到它: 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
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------



數(shù)據(jù)分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機(jī)數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進(jìn)行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個參數(shù)驗證碼對象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗服務(wù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時表示是新驗證碼的宕機(jī) product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計時完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }