Python:win32com GetFileVersion 不起作用(pywintypes.com_error)

问题描述 投票:0回答:1

我已经安装了python模块pywin32(-m pip install --upgrade pywin32) 我想知道使用此模块的 .exe 的版本。我在互联网上搜索,总是找到与我正在使用的相同的代码示例:

from win32com.client import Dispatch

def get_version_number(file_path): 

   information_parser = Dispatch("Scripting.FileSystemObject") 
   version = information_parser.GetFileVersion(file_path) 
   return version 

file_path = r'‪C:\Users\Alex\Downloads\ChromeSetup.exe'
version = get_version_number(file_path) 

print(version)

我收到此错误消息:

version = information_parser.GetFileVersion(file_path)
File "<COMObject Scripting.FileSystemObject>", line 3, in GetFileVersion
pywintypes.com_error: (-2147352567, 'Ocurrió una excepción.', (0, None, None, None, 0, -2147024894), None)

我不知道问题所在,任何 .exe 都会发生这种情况。 怎么了?有什么解决办法吗? 谢谢。

python exception exe pywin32 win32com
1个回答
0
投票

试试这个:

from win32api import GetFileVersionInfo, LOWORD, HIWORD
def get_version_number (filename):
    try:
        info = GetFileVersionInfo (filename, "\\")
        ms = info['FileVersionMS']
        ls = info['FileVersionLS']
        return HIWORD (ms), LOWORD (ms), HIWORD (ls), LOWORD (ls)
    except:
        return 0,0,0,0

file_path = r'‪C:\Users\Alex\Downloads\ChromeSetup.exe'
version = get_version_number(file_path)
version_str = ''.join([str(ele) + "." for ele in version])
print(version_str)

它使用 win32 api 而不是 com

© www.soinside.com 2019 - 2024. All rights reserved.