gpt4 book ai didi

c++ - 如何用 Python 判断一个文件在 Windows 上是否可执行?

转载 作者:太空狗 更新时间:2023-10-29 21:44:49 26 4
gpt4 key购买 nike

我正在写 grepath%PATH% 中查找与模式匹配的可执行文件的实用程序。我需要定义路径中给定的文件名是否可执行(重点是命令行脚本)。

基于 "Tell if a file is executable"我有:

import os
from pywintypes import error
from win32api import FindExecutable, GetLongPathName

def is_executable_win(path):
try:
_, executable = FindExecutable(path)
ext = lambda p: os.path.splitext(p)[1].lower()
if (ext(path) == ext(executable) # reject *.cmd~, *.bat~ cases
and samefile(GetLongPathName(executable), path)):
return True
# path is a document with assoc. check whether it has extension
# from %PATHEXT%
pathexts = os.environ.get('PATHEXT', '').split(os.pathsep)
return any(ext(path) == e.lower() for e in pathexts)
except error:
return None # not an exe or a document with assoc.

samefile 是:

try: samefile = os.path.samefile
except AttributeError:
def samefile(path1, path2):
rp = lambda p: os.path.realpath(os.path.normcase(p))
return rp(path1) == rp(path2)

如何在给定的上下文中改进 is_executable_win? Win32 API 中的哪些函数可以提供帮助?

附言

  • 时间表现并不重要
  • subst 驱动器和 UNC,unicode 路径不在考虑范围内
  • 如果使用 Windows XP 上可用的函数,C++ 答案是可以的

例子

  • notepad.exe 是可执行的(通常)
  • 如果
  • which.py​​ 与某些可执行文件(例如 python.exe)关联并且 .PY 位于 %PATHEXT% 中,则它是可执行文件'C:\> which' 可以开始:

    some\path\python.exe another\path\in\PATH\which.py
  • somefile.doc 很可能不是可执行文件(例如,当它与 Word 关联时)

  • another_file.txt 不可可执行(通常)
  • 如果
  • ack.pl 与某个可执行文件(很可能是 perl.exe)关联并且 .PL%PATHEXT% 中,则它是可执行的>(即如果扩展名在路径中,我可以运行 ack 而无需指定扩展名)

本题中什么是“可执行”

def is_executable_win_destructive(path):
#NOTE: it assumes `path` <-> `barename` for the sake of example
barename = os.path.splitext(os.path.basename(path))[0]
p = Popen(barename, stdout=PIPE, stderr=PIPE, shell=True)
stdout, stderr = p.communicate()
return p.poll() != 1 or stdout != '' or stderr != error_message(barename)

error_message() 取决于语言。英文版是:

def error_message(barename):
return "'%(barename)s' is not recognized as an internal" \
" or external\r\ncommand, operable program or batch file.\r\n" \
% dict(barename=barename)

如果 is_executable_win_destructive() 在定义路径是否指向可执行文件时返回,用于本题目的。

例子:

>>> path = r"c:\docs\somefile.doc"
>>> barename = "somefile"

之后它执行 %COMSPEC%(默认为 cmd.exe):

c:\cwd> cmd.exe /c somefile

如果输出是这样的:

'somefile' is not recognized as an internal or externalcommand, operable program or batch file.

Then the path is not an executable else it is (lets assume there is one-to-one correspondence between path and barename for the sake of example).

Another example:

>>> path = r'c:\bin\grepath.py'
>>> barename = 'grepath'

如果 %PATHEXT% 中的 .PY%PATH% 中的 c:\bin 则:

c:\docs> grepath
Usage:
grepath.py [options] PATTERN
grepath.py [options] -e PATTERN

grepath.py: error: incorrect number of arguments

以上输出不等于 error_message(barename) 因此 'c:\bin\grepath.py' 是一个“可执行文件”。

所以问题是如何在不实际运行的情况下找出 path 是否会产生错误?什么 Win32 API 函数和什么条件用于触发“未被识别为内部..”错误?

最佳答案

shoosh 打败了我 :)

如果我没记错的话,您应该尝试阅读文件中的前 2 个字符。如果你返回“MZ”,你就有了一个 exe。


hnd = open(file,"rb")
if hnd.read(2) == "MZ":
print "exe"

关于c++ - 如何用 Python 判断一个文件在 Windows 上是否可执行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/646955/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com