Reputation: 87
I want to use python to get the executable file version, and i know of pefile.py
how to use it to do this?
notes: the executable file may be not complete.
Upvotes: 3
Views: 5231
Reputation: 22047
Assuming by "executable file version" you mean a) on Windows, b) the information shown in the Properties, Details tab, under "File version", you can retrieve this using the pywin32 package with a command like the following:
>>> import win32api as w
>>> hex(w.GetFileVersionInfo('c:/windows/regedit.exe', '\\')['FileVersionMS'])
'0x60000'
>>> hex(w.GetFileVersionInfo('c:/windows/regedit.exe', '\\')['FileVersionLS'])
'0x17714650'
Note that 0x60000 has the major/minor numbers (6.0) and 0x17714650 is the next two, which if taken as two separate words (0x1771 and 0x4650, or 6001 and 18000 in decimal) correspond to the values on my machine, where regedit's version is 6.0.6001.18000.
Upvotes: 2
Reputation: 21269
Here's a complete example script that does what you want:
import sys
def main(pename):
from pefile import PE
pe = PE(pename)
if not 'VS_FIXEDFILEINFO' in pe.__dict__:
print "ERROR: Oops, %s has no version info. Can't continue." % (pename)
return
if not pe.VS_FIXEDFILEINFO:
print "ERROR: VS_FIXEDFILEINFO field not set for %s. Can't continue." % (pename)
return
verinfo = pe.VS_FIXEDFILEINFO
filever = (verinfo.FileVersionMS >> 16, verinfo.FileVersionMS & 0xFFFF, verinfo.FileVersionLS >> 16, verinfo.FileVersionLS & 0xFFFF)
prodver = (verinfo.ProductVersionMS >> 16, verinfo.ProductVersionMS & 0xFFFF, verinfo.ProductVersionLS >> 16, verinfo.ProductVersionLS & 0xFFFF)
print "Product version: %d.%d.%d.%d" % prodver
print "File version: %d.%d.%d.%d" % filever
if __name__ == '__main__':
if len(sys.argv) != 2:
sys.stderr.write("ERROR:\n\tSyntax: verinfo <pefile>\n")
sys.exit(1)
sys.exit(main(sys.argv[1]))
The relevant lines being:
verinfo = pe.VS_FIXEDFILEINFO
filever = (verinfo.FileVersionMS >> 16, verinfo.FileVersionMS & 0xFFFF, verinfo.FileVersionLS >> 16, verinfo.FileVersionLS & 0xFFFF)
prodver = (verinfo.ProductVersionMS >> 16, verinfo.ProductVersionMS & 0xFFFF, verinfo.ProductVersionLS >> 16, verinfo.ProductVersionLS & 0xFFFF)
all of which happens only after checking that we have something meaningful in these properties.
Upvotes: 3
Reputation: 39838
The version numbers of Windows programs are stored in the resource section of the program file, not in the PE format header. I'm not familiar with pefile.py
, so I don't know whether it directly handles resource sections too. If not, you should be able to find the information you need for that in this MSDN article.
Upvotes: 0