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)