Python3 检测文件真实类型(walker)

测试环境

Windows  10
Python   3.6.7
filetype 1.0.5

安装 filetype

pip3 install filetype -i https://pypi.doubanio.com/simple/

示例

code

#encoding: utf-8
#author: walker
#date: 2019-11-08
#summary: 检测(/猜测)真实文件类型

import os
import filetype

def GuessFileType(pathfile):    
    print('GuessFileType %s ...' % pathfile)
    kind = filetype.guess(pathfile)
    print('File name: %s' % os.path.basename(pathfile))
    print('File extension: %s' % kind.extension)
    print('File MIME type: %s' % kind.mime)

if __name__ == '__main__':
    pathfile = r'D:\Python3Project\test\dir.tar.gz'
    GuessFileType(pathfile)

运行

D:\Python3Project\test>python3 t.py
GuessFileType D:\Python3Project\test\dir.tar.gz ...
File name: dir.tar.gz
File extension: zip
File MIME type: application/zip

相关链接

  • filetype GitHub 网址: https://github.com/h2non/filetype.py
  • 检测工具: TrIDNet - File Identifier
本文出自 walker snapshot

你可能感兴趣的