如何从Python获取应用程序版本?
基本上,我想知道用户当前安装的ArcGIS版本是什么。我查看了注册表,但没有找到与版本相关的信息。不过我知道这个版本信息是存储在.exe文件里的。
我在网上搜索了很多,但没有找到什么有用的东西。我尝试使用GetFileVersionInfo这个方法,但得到的信息似乎杂乱无章。
有没有什么好的主意?
补充说明
唉……
结果发现pywin32并不是所有机器上都安装的。有没有人知道通过ctypes能否做到同样的事情?
另外,这个问题只适用于Windows系统。
2 个回答
0
有一个叫做“strings”的工具,它是GNU Linux系统中的一个小程序,可以用来显示任何文件中可打印的字符,不管这个文件是二进制的还是非二进制的。你可以试着用这个工具,看看能不能找到类似版本号的内容。
在Windows系统上,你可以在这里下载strings工具:http://unxutils.sourceforge.net/
2
如果你不想使用pywin32来实现这个功能,其实可以用ctypes来完成。
关键在于要搞懂那个返回的文件版本结构,真的是有点复杂。
有一篇老邮件列表的帖子正好在做你想做的事情。可惜我现在手边没有Windows电脑,不能亲自测试。不过即使它不管用,至少也能给你一个不错的起点。
这里有代码,万一那些2006年的存档消失了也好有个备份:
import array
from ctypes import *
def get_file_info(filename, info):
"""
Extract information from a file.
"""
# Get size needed for buffer (0 if no info)
size = windll.version.GetFileVersionInfoSizeA(filename, None)
# If no info in file -> empty string
if not size:
return ''
# Create buffer
res = create_string_buffer(size)
# Load file informations into buffer res
windll.version.GetFileVersionInfoA(filename, None, size, res)
r = c_uint()
l = c_uint()
# Look for codepages
windll.version.VerQueryValueA(res, '\\VarFileInfo\\Translation',
byref(r), byref(l))
# If no codepage -> empty string
if not l.value:
return ''
# Take the first codepage (what else ?)
codepages = array.array('H', string_at(r.value, l.value))
codepage = tuple(codepages[:2].tolist())
# Extract information
windll.version.VerQueryValueA(res, ('\\StringFileInfo\\%04x%04x\\'
+ info) % codepage, byref(r), byref(l))
return string_at(r.value, l.value)
print get_file_info(r'C:\WINDOWS\system32\calc.exe', 'FileVersion')
--
好吧,我又回到Windows电脑旁边了。其实我现在已经试过这段代码了。“对我来说是有效的”。
>>> print get_file_info(r'C:\WINDOWS\system32\calc.exe', 'FileVersion')
6.1.7600.16385 (win7_rtm.090713-1255)