失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > python比较两个二进制文件_python三种方法判断文件是否为二进制文件

python比较两个二进制文件_python三种方法判断文件是否为二进制文件

时间:2020-09-19 16:49:58

相关推荐

python比较两个二进制文件_python三种方法判断文件是否为二进制文件

两种方法判断文件是否为二进制文件,最准确的就是把这两种方法结合起来。

(1)方法一:

它首先检查文件是否以BOM开始,如果不在初始8192字节内查找零字节:

import codecs

file_path = "/home/ubuntu/zgd/ztest/_gs418_510txp_v6.6.2.7.stk.extracted/test"

#: BOMs to indicate that a file is a text file even if it contains zero bytes.

_TEXT_BOMS = (

codecs.BOM_UTF16_BE,

codecs.BOM_UTF16_LE,

codecs.BOM_UTF32_BE,

codecs.BOM_UTF32_LE,

codecs.BOM_UTF8,

)

def is_binary_file(file_path):

with open(file_path, 'rb') as file:

initial_bytes = file.read(8192)

file.close()

return not any(initial_bytes.startswith(bom) for bom in _TEXT_BOMS) and b'\0' in initial_bytes

if __name__ == "__main__":

print is_binary_file(file_path)

上面is_binary_file()函数也可以改成下面的方式:

def is_binary_file(file_path):

with open(file_path, 'rb') as file:

initial_bytes = file.read(8192)

file.close()

for bom in _TEXT_BOMS:

if initial_bytes.startswith(bom):

continue

else:

if b'\0' in initial_bytes:

return True

return False

(2)方法二:

使用magic

pip install python-magic

def getFileType(ff):

mime_kw = 'x-executable|x-sharedlib|octet-stream|x-object' ###可执行文件、链接库、动态流、对象

try:

magic_mime = magic.from_file(ff, mime=True)

magic_hit = re.search(mime_kw, magic_mime, re.I)

if magic_hit:

return True

else:

return False

except Exception, e:

print e.message

较好的方法是对两种类型同时进行处理:

# -*- coding:utf-8 -*-

# @Author:zgd

# @time:/6/21

# @File:operateSystem.py

import magic

import re

import codecs

def is_binary_file_1(ff):

'''

根据text文件数据类型判断是否是二进制文件

:param ff: 文件名(含路径)

:return: True或False,返回是否是二进制文件

'''

TEXT_BOMS = (

codecs.BOM_UTF16_BE,

codecs.BOM_UTF16_LE,

codecs.BOM_UTF32_BE,

codecs.BOM_UTF32_LE,

codecs.BOM_UTF8,

)

with open(file_path, 'rb') as file:

CHUNKSIZE = 8192

initial_bytes = file.read(CHUNKSIZE)

file.close()

#: BOMs to indicate that a file is a text file even if it contains zero bytes.

return not any(initial_bytes.startswith(bom) for bom in TEXT_BOMS) and b'\0' in initial_bytes

def is_binwary_file_2(ff):

'''

根据magic文件的魔术判断是否是二进制文件

:param ff: 文件名(含路径)

:return: True或False,返回是否是二进制文件

'''

mime_kw = 'x-executable|x-sharedlib|octet-stream|x-object' ###可执行文件、链接库、动态流、对象

try:

magic_mime = magic.from_file(ff, mime=True)

magic_hit = re.search(mime_kw, magic_mime, re.I)

if magic_hit:

return True

else:

return False

except Exception, e:

return False

if __name__ == "__main__":

file_path = "/home/ubuntu/zgd/ztest/_gs418_510txp_v6.6.2.7.stk.extracted/D0"

print is_binary_file_1(file_path)

print is_binwary_file_2(file_path)

print any((is_binary_file_1(file_path), is_binwary_file_2(file_path)))

(3)方法三:

获取ELF标识信息。

根据文件中是否有ELF头进行判断文件是否为二进制文件

# 判断文件是否是elf文件

def is_ELFfile(filepath):

if not os.path.exists(filepath):

logger.info('file path {} doesnot exits'.format(filepath))

return False

# 文件可能被损坏,捕捉异常

try:

FileStates = os.stat(filepath)

FileMode = FileStates[stat.ST_MODE]

if not stat.S_ISREG(FileMode) or stat.S_ISLNK(FileMode): # 如果文件既不是普通文件也不是链接文件

return False

with open(filepath, 'rb') as f:

header = (bytearray(f.read(4))[1:4]).decode(encoding="utf-8")

# logger.info("header is {}".format(header))

if header in ["ELF"]:

# print header

return True

except UnicodeDecodeError as e:

# logger.info("is_ELFfile UnicodeDecodeError {}".format(filepath))

# logger.info(str(e))

pass

return False

参考链接:

/code-/zh-CN/q/db66d.html

/question/153346

/limodou/archive//08/30/83538.aspx

/xc_zhou/article/details/86544346

如果觉得《python比较两个二进制文件_python三种方法判断文件是否为二进制文件》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。