python如何判断二进制文件

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

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

#!/usr/bin/env python3
# encoding: utf-8
# coding style: pep8
# ====================================================
#   Copyright (C)2020 All rights reserved.
#
#   Author        : xxx
#   Email         : xxx@gmail.com
#   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	# 二进制文件的输出结果

参考:

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值