通过pywin32中的win32api.GetFileVersionInfo来获取

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Get the version information from Windows PE

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

def printOtherInfo(filename):
try:
info = GetFileVersionInfo(filename, "\")
print(info)

# In order to get the company information, we need to get the lang
# and code page first, then get the related strings
trans = GetFileVersionInfo(filename, "\VarFileInfo\Translation")
if not trans:
return
print(info)

# Common string lists:
# "CompanyName","FileDescription", "FileVersion", "InternalName",
# "LegalCopyright", "OriginalFilename", "ProductName", "ProductVersion"
# You can use this way to get custom defined strings.

info = GetFileVersionInfo(filename, "\StringFileInfo\%04x%04x\%s" % (trans[0][0], trans[0][1], "CompanyName"))
print(info)
info = GetFileVersionInfo(filename, "\StringFileInfo\%04x%04x\%s" % (trans[0][0], trans[0][1], "FileVersion"))
print(info)
except:
pass

if __name__ == '__main__':
import os
filename = os.environ["COMSPEC"]
print ".".join ([str (i) for i in get_version_number (filename)])
printOtherInfo(filename)
知识共享授权条款
本著作Chen, Zai-Chun制作,以知识共享 姓名标示-相同方式分享 4.0 国际 授权条款释出

留言

Dec 12 2010