Python: win32com GetFileVersion not working (pywintypes.com_error)

47 Views Asked by At

I have installed the python module pywin32 (-m pip install --upgrade pywin32) I want to know the version of a .exe using this module. I search on internet and I always find the same code sample that I'm using:

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)

I have this error message:

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)

I don't know the problem, it happens with any .exe. What is happening? Any solution? Thanks.

1

There are 1 best solutions below

0
Misiu On BEST ANSWER

try this:

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)

It uses the win32 api instead of the com one