Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何检查文件是否是来自Python的macOS可执行文件?_Python_Macos - Fatal编程技术网

如何检查文件是否是来自Python的macOS可执行文件?

如何检查文件是否是来自Python的macOS可执行文件?,python,macos,Python,Macos,例如,假设: $cd/Applications/Xcode.app/Contents/MacOS $file Xcode Xcode:Mach-O 64位可执行文件x86_64 以及: $cd/Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/Versions/A $file IDEKit IDEKit:Mach-O 64位动态链接共享库x86_64 我希望能够做file正在做的事情,特别是检查一个文件是否是可执行文件,而

例如,假设:

$cd/Applications/Xcode.app/Contents/MacOS
$file Xcode
Xcode:Mach-O 64位可执行文件x86_64
以及:

$cd/Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/Versions/A
$file IDEKit
IDEKit:Mach-O 64位动态链接共享库x86_64
我希望能够做
file
正在做的事情,特别是检查一个文件是否是可执行文件,而是通过Python编程

我知道我可以调用
file
命令并从Python解析结果,但是有没有更好的方法不涉及调用
file

请注意:

$ls-l IDEKit
-rwxr-xr-x 1根轮17256912 Apr 5 17:42 IDEKit*
以及:

$ls-l Xcode
-rwxr-xr-x 1根轮44416 Apr 11 13:40 Xcode*
i、 例如,就文件系统权限位而言,它们都是“可执行的”,但只有
Xcode
才是真正的可执行文件。

您可以使用
文件的官方python绑定

安装后,您只需检查文件即可

导入魔法
detected=magic.detected\u from\u文件名('magic.py'))
打印“检测到的MIME类型:{}”。格式(检测到的.MIME_类型)
打印“检测到的编码:{}”。格式(检测到的。编码)
打印“检测到的文件类型名称:{}”。格式(检测到的.name)
您可以使用
文件的官方python绑定

安装后,您只需检查文件即可

导入魔法
detected=magic.detected\u from\u文件名('magic.py'))
打印“检测到的MIME类型:{}”。格式(检测到的.MIME_类型)
打印“检测到的编码:{}”。格式(检测到的。编码)
打印“检测到的文件类型名称:{}”。格式(检测到的.name)
事实证明,允许您从Python读取和检查文件的Mach-O头(如果有)。所以代码如下:

def read_macho_headers( file ):
    try:
        return MachO.MachO( file ).headers
    except Exception:                   # not a Mach-O file
        return None

def is_macho_exe( macho_headers ):
    filetype = macho_headers[0].header.filetype
    return filetype == mach_o.MH_EXECUTE
将起作用。

事实证明,允许您从Python读取和检查文件的Mach-O头(如果有)。所以代码如下:

def read_macho_headers( file ):
    try:
        return MachO.MachO( file ).headers
    except Exception:                   # not a Mach-O file
        return None

def is_macho_exe( macho_headers ):
    filetype = macho_headers[0].header.filetype
    return filetype == mach_o.MH_EXECUTE

将起作用。

一种方法是读取文件幻数,并将其与如下列表进行比较:。或者只需查看
文件的源代码,看看它是如何实现的。@对于Mach-O 64位可执行文件&dylib,迷惑不解的幻数是相同的。这是一个深入的好答案,而不是Python。@Kamil.S,很有趣,谢谢你的链接。一种方法是读取一个文件幻数,并将其与如下列表进行比较:。或者只需查看
文件的源代码,看看它是如何实现的。@对于Mach-O 64位可执行文件&dylib,迷惑不解的幻数是相同的。这是一个很好的答案,可以更深入,但不是Python。@Kamil.S,很有趣,谢谢你的链接。