python常用库之filetype | python判断文件的类型

本文介绍了一种使用Python根据文件头部的十六进制特征码来识别文件真实格式的方法,并提供了具体的代码实现。此外还介绍了第三方库filetype的安装及使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

python判断文件的类型

需求背景

判断文件类型在开发中非常常见的需求,怎样才能准确的判断文件类型呢?

判断文件的头,每种文件在文件的头中会标识这种文件的类型。

对一个文件来说,文件的后缀是可以随意修改的,但是如果改的时间比较长,可能就不知道原本的文件格式是啥,不匹配的文件格式,在Windows上,大多数情况是无法正常打开的。因此可以根据文件前几个字符的特征码来判断文件真实的格式

用16进制字符串的目的是可以知道文件头是多少字节# 各种文件头的长度不一样,少则2字符,长则8字符

python 根据文件头判断文件类型

python3通过文件头判断文件类型
参考URL: https://blog.youkuaiyun.com/privateobject/article/details/78069500

import os

# 支持文件类型
# 用16进制字符串的目的是可以知道文件头是多少字节
# 各种文件头的长度不一样,少半2字符,长则8字符

# 支持文件类型
# 用16进制字符串的目的是可以知道文件头是多少字节
# 各种文件头的长度不一样,少半2字符,长则8字符
def typeList():
    print('获取文件格式十六进制码表……');
    return {
        "d0cf11e0a1b11ae10000": 'xls',
        '504b030414': 'docx_docx_xlsx',   #docx, pptx, xlsx
    }

# 字节码转16进制字符串
def bytes2hex(bytes):
    #print('关键码转码……');
    num = len(bytes)
    hexstr = u""
    for i in range(num):
        t = u"%x" % bytes[i]
        if len(t) % 2:
            hexstr += u"0"
        hexstr += t
    return hexstr.upper()


# 获取文件类型
def filetype(filename):
    # docx文件内容为空时,这里会返回
    if os.path.getsize(filename) == 0:
       print("文件内容为空")
       return "xxxx"

    print('读文件二进制码中……');
    binfile = open(filename, 'rb') # 必需二制字读取
    print('读取20字节关键码……');
    bins = binfile.read(20) #提取20个字符
    binfile.close() #关闭文件流
    bins = bytes2hex(bins) #转码
    bins = bins.lower()#小写
    print(bins);
    tl = typeList()  #文件类型
    ftype = 'unknown'
    print('关键码比对中……');
    for hcode in tl.keys():
        lens = len(hcode) # 需要的长度
        if bins[0:lens] == hcode:
            ftype = tl[hcode]
            break
    if ftype == 'unknown':#全码未找到,优化处理,码表取5位验证
        bins = bins[0:5];
        for hcode in tl.keys():
            if len(hcode) > 5 and bins == hcode[0:5]:
                ftype = tl[hcode]
                break
    return ftype


if __name__ == '__main__':
    pathfile = r'D:\tmp\111.xlsx'
    #pathfile = r'D:\tmp\2222.doc'
    #pathfile = r'D:\tmp\3333.docx'
    #pathfile = r'D:\tmp\444.ppt'
    #pathfile = r'D:\tmp\666.pptx'
    #pathfile = r'D:\tmp\sss.txt'

    ftype = filetype(pathfile)
    print(ftype)

常见文件格式的文件头

文件格式 文件头(十六进制)

JPEG (jpg) FFD8FF 
PNG (png) 89504E47 
GIF (gif) 47494638 
TIFF (tif) 49492A00 
Windows Bitmap (bmp) 424D 
CAD (dwg) 41433130 
Adobe Photoshop (psd) 38425053 
Rich Text Format (rtf) 7B5C727466 
XML (xml) 3C3F786D6C 
HTML (html) 68746D6C3E 
Email [thorough only] (eml) 44656C69766572792D646174653A 
Outlook Express (dbx) CFAD12FEC5FD746F 
Outlook (pst) 2142444E 
MS Word/Excel (xls.or.doc) D0CF11E0 
MS Access (mdb) 5374616E64617264204A

神奇数字签名: https://en.wikipedia.org/wiki/Magic_number_(programming)#Magic_numbers_in_files

三方库 filetype

小型无依赖 Python 包,用于推断文件类型和 MIME 类型,检查文件或缓冲区的神奇数字签名。

filetype模块支持60余种文件类型,下表列出不同文件类型的扩展名

pip install filetype
import os
import filetype

def GuessFileType(pathfile):
    print('GuessFileType %s ...' % pathfile)

    kind = filetype.guess(pathfile)
    if kind is None:
        print('Cannot guess file type!')
        return

    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:\tmp\111.xlsx'
    #pathfile = r'D:\tmp\2222.doc'
    #pathfile = r'D:\tmp\3333.docx'
    pathfile = r'D:\tmp\444.ppt'
    #pathfile = r'D:\tmp\666.pptx'

    GuessFileType(pathfile)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

西京刀客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值