python如何判断二进制文件

最近,由于坑爹的二进制转换工具,导致部分资源转换不成功,现在需要将失败的包筛选出来,所以看了下判断二进制文件的方法,记录如下。

方法一、二:
通过python脚本判断

#!/usr/bin/env python3
# encoding: utf-8
# coding style: pep8
# ====================================================
#   Copyright (C)2020 All rights reserved.
#
#   Author        : xxx
#   Email         : [email protected]
#   File Name     : check_bin.py
#   Last Modified : 2020-03-31 11:57
#   Description   :
#
# ====================================================

import sys
import os
import magic
import re
import codecs

COMMENT_SWITCH=False

sum_count = 0
problem_count = 0




#: 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 lm_check_bin_file_1(file_dir):
    with open(file_dir, '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

def lm_check_bin_file_2(file_dir):
    bin_type = 'x-executable|x-sharedlib|octet-stream|x-object'  #二进制文件类型
    try:
        file_type = magic.from_file(file_dir, mime=True)
        b_flag = re.search(bin_type, file_type, re.I)
        if b_flag:
            return True
        else:
            return False
    except Exception as e:
        print(e.message)

方法三:
通过file命令的返回值判断。如何在python中获取shell命令返回值?

file -r do.sh	
# do.sh: Bourne-Again shell script text executable, UTF-8 Unicode text
file -r test.py
# test.py: Python script text executable, ASCII text
file -r content.json
# content.json: ASCII text
file -r main.scene
# main.scene: data	# 二进制文件的输出结果

参考:

猜你喜欢

转载自blog.csdn.net/zxcasd11/article/details/105249142