python获取文件版本信息、公司名和产品名的方法
本文实例讲述了python获取文件版本信息、公司名和产品名的方法,分享给大家供大家参考。具体如下:
该python代码可得到文件版本信息、公司名和产品名。其他的信息都在返回的字典中。具体代码如下:
def_getCompanyNameAndProductName(self,file_path):
"""
Readallpropertiesofthegivenfilereturnthemasadictionary.
"""
propNames=('Comments','InternalName','ProductName',
'CompanyName','LegalCopyright','ProductVersion',
'FileDescription','LegalTrademarks','PrivateBuild',
'FileVersion','OriginalFilename','SpecialBuild')
props={'FixedFileInfo':None,'StringFileInfo':None,'FileVersion':None}
try:
#backslashasparmreturnsdictionaryofnumericinfocorrespondingtoVS_FIXEDFILEINFOstruc
fixedInfo=win32api.GetFileVersionInfo(file_path,'\\')
props['FixedFileInfo']=fixedInfo
props['FileVersion']="%d.%d.%d.%d"%(fixedInfo['FileVersionMS']/65536,
fixedInfo['FileVersionMS']%65536,fixedInfo['FileVersionLS']/65536,
fixedInfo['FileVersionLS']%65536)
#\VarFileInfo\Translationreturnslistofavailable(language,codepage)
#pairsthatcanbeusedtoretreivestringinfo.Weareusingonlythefirstpair.
lang,codepage=win32api.GetFileVersionInfo(file_path,'\\VarFileInfo\\Translation')[0]
#anyothermustbeoftheform\StringfileInfo\%04X%04X\parm_name,middle
#twoarelanguage/codepagepairreturnedfromabove
strInfo={}
forpropNameinpropNames:
strInfoPath=u'\\StringFileInfo\\%04X%04X\\%s'%(lang,codepage,propName)
##printstr_info
strInfo[propName]=win32api.GetFileVersionInfo(file_path,strInfoPath)
props['StringFileInfo']=strInfo
except:
pass
ifnotprops["StringFileInfo"]:
return(None,None)
else:
return(props["StringFileInfo"]["CompanName"],props["StringFileInfo"]["ProductName"])
希望本文所述对大家的Python程序设计有所帮助。