python准确判断文件类型
判断文件类型在开发中非常常见的需求,怎样才能准确的判断文件类型呢?首先大家想到的是文件的后缀,但是非常遗憾的是这种方法是非常不靠谱的,因为文件的后缀是可以随意更改的,而大家都知道后缀在linux系统下是没有这个概念的,所以仅靠判断后缀无法准确判断一个文件的类型。还有第二种方法是判断文件的头,每种文件在文件的头中会标识这种文件的类型,下面我们来看看如何用python来判断文件的类型。
python通过文件头判断文件类型的方法:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
#! /usr/bin/python # pythontab提醒您注意中文编码问题,指定编码为utf-8 # -*- coding: utf-8 -*- import struct # 支持文件类型 # 用16进制字符串的目的是可以知道文件头是多少字节 # 各种文件头的长度不一样,少则2字符,长则8字符 def typeList(): return { "FFD8FF": "JPEG", "89504E47": "PNG"} # 字节码转16进制字符串 def bytes2hex(bytes): 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): binfile = open(filename, 'rb') # 必需二制字读取 tl = typeList() ftype = 'unknown' for hcode in tl.keys(): numOfBytes = len(hcode) / 2 # 需要读多少字节 binfile.seek(0) # 每次读取都要回到文件头,不然会一直往后读取 hbytes = struct.unpack_from("B"*numOfBytes, binfile.read(numOfBytes)) # 一个 "B"表示一个字节 f_hcode = bytes2hex(hbytes) if f_hcode == hcode: ftype = tl[hcode] break binfile.close() return ftype if __name__ == '__main__': print filetype('./test.jpg') |
常见文件格式的文件头
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
文件格式 文件头(十六进制) 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 |
http://www.qytang.com/
http://www.qytang.com/cn/list/41/
http://www.qytang.com/cn/list/37/
http://www.qytang.com/cn/list/46/
http://www.qytang.com/cn/page/19.htm
http://www.qytang.com/cn/list/32/
http://www.qytang.com/cn/list/28/
http://www.qytang.com/cn/list/25/
http://www.qytang.com/cn/list/28/625.htm
http://www.qytang.com/cn/list/28/612.htm
http://www.qytang.com/cn/list/28/611.htm
http://www.qytang.com/cn/list/28/610.htm
python准确判断文件类型的更多相关文章
- Python使用filetype精确判断文件类型
Python使用filetype精确判断文件类型 判断文件类型在开发中非常常见的需求,怎样才能准确的判断文件类型呢?首先大家想到的是文件的后缀,但是非常遗憾的是这种方法是非常不靠谱的,因为文件的后缀是 ...
- Python之基于十六进制判断文件类型
核心代码: #!/usr/bin/env python # -*- coding: utf-8 -*- # @Author : suk import struct from io import Byt ...
- 使用apache.tika判断文件类型
一. 判断文件类型一般可采用两种方式 1. 后缀名判断 简单易操作,但无法准确判断类型 2. 文件头信息判断 通常可以判断文件类型,但有些文件类型无法判断(如word和excel头信息的前几个字节是一 ...
- JavaScript根据文件名判断文件类型
//JavaScript根据文件名判断文件类型 var imgExt = new Array(".png",".jpg",".jpeg",& ...
- 利用PHP取二进制文件头判断文件类型
<?php $files = array('D:\no.jpg', 'D:\no.png','D:\no2.JPEG','D:\no.BMP'); $fileTypes = array( 779 ...
- Linux中用st_mode判断文件类型
Linux中用st_mode判断文件类型 2012-12-11 12:41 14214人阅读 评论(4) 收藏 举报 分类: Linux(8) C/C++(20) 版权声明:本文为博主原创文章, ...
- php 读取文件头判断文件类型的实现代码
php代码实现读取文件头判断文件类型,支持图片.rar.exe等后缀. 例子: <?php $filename = "11.jpg"; //为图片的路径可以用d:/uploa ...
- PHP取二进制文件头快速判断文件类型的实现代码
通过读取文件头信息来识别文件的真实类型. 一般我们都是按照文件扩展名来判断文件类型,但是这个很不靠谱,轻易就通过修改扩展名来躲避了,一般必须要读取文件信息来识别,PHP扩展中提供了类似 exif_im ...
- PHP取二进制文件头快速判断文件类型
<?php /*文件扩展名说明 *7173 gif *255216 jpg *13780 png *6677 bmp *239187 txt,aspx,asp,sql *208207 xls.d ...
随机推荐
- 1103 Integer Factorization
题意:给出一个正整数N,以及k,p,要求把N分解成k个因式,即N=n1^p + n2^p + ... + nk^p.要求n1,n2,...按降序排列,若有多个解,则选择n1+n2+...+nk最大的, ...
- C++ 单链表操作总结
第一.单链表的定义和操作 #include <iostream> using namespace std; template <typename T> struct Node ...
- gil基本介绍
一 引子 定义: In CPython, the global interpreter lock, or GIL, is a mutex that prevents multiple native t ...
- oracke创建db link
-- Create database link create public database link test_link connect to 用户名 identified by 密码 using ...
- OD 实验(二十) - 对反调试程序的逆向分析(一)
程序: Keyfile.dat 里的内容 该文件中要至少有 9 个 ReverseMe.A: 运行程序 用 OD 打开该程序,运行 弹出的是错误的对话框 该程序发现 OD 对它的调试,所以该程序对 O ...
- 转:Numpy教程
因为用到theano写函数的时候饱受数据结构困扰 于是上网找了一篇numpy教程(theano的数据类型是基于numpy的) 原文排版更好,阅读体验更佳: http://phddreamer.blog ...
- angularjs之级联菜单
原理: 1.ng-options中val.id as val.name for val in cascading 将id的值赋给 mg-modelone2.在通过ng-change传给函数3.当一级下 ...
- 「小程序JAVA实战」小程序上传短视频(46)
转自:https://idig8.com/2018/09/14/xiaochengxujavashizhanxiaochengxushangchuanduanshipin45/ 个人信息:用户上传短视 ...
- WPF 绑定以基础数据类型为集合的无字段名的数据源
WPF 绑定以基础数据类型为集合的无字段名的数据源 运行环境:Window7 64bit,.NetFramework4.61,C# 6.0: 编者:乌龙哈里 2017-02-21 我们在控件的数据绑定 ...
- 利用同步辅助类CountDownLatch计算多线程的运行时间
一.CountDownLatch jdk提供的一个同步辅助类,在完成一组在在其他线程中执行的操作前,允许一个或者多个其他的线程等待,通过调用 await() 方法阻塞,直到由于 countDown() ...