python获取文件版本信息、公司名和产品名的方法

yipeiwu_com5年前Python基础

本文实例讲述了python获取文件版本信息、公司名和产品名的方法,分享给大家供大家参考。具体如下:

该python代码可得到文件版本信息、公司名和产品名。其他的信息都在返回的字典中。具体代码如下:

  def _getCompanyNameAndProductName(self, file_path): 
    """ 
    Read all properties of the given file return them as a dictionary. 
    """ 
    propNames = ('Comments', 'InternalName', 'ProductName', 
      'CompanyName', 'LegalCopyright', 'ProductVersion', 
      'FileDescription', 'LegalTrademarks', 'PrivateBuild', 
      'FileVersion', 'OriginalFilename', 'SpecialBuild') 
   
    props = {'FixedFileInfo': None, 'StringFileInfo': None, 'FileVersion': None} 
   
    try: 
      # backslash as parm returns dictionary of numeric info corresponding to VS_FIXEDFILEINFO struc 
      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\Translation returns list of available (language, codepage) 
      # pairs that can be used to retreive string info. We are using only the first pair. 
      lang, codepage = win32api.GetFileVersionInfo(file_path, '\\VarFileInfo\\Translation')[0] 
   
      # any other must be of the form \StringfileInfo\%04X%04X\parm_name, middle 
      # two are language/codepage pair returned from above 
   
      strInfo = {} 
      for propName in propNames: 
        strInfoPath = u'\\StringFileInfo\\%04X%04X\\%s' % (lang, codepage, propName) 
        ## print str_info 
        strInfo[propName] = win32api.GetFileVersionInfo(file_path, strInfoPath) 
   
      props['StringFileInfo'] = strInfo 
    except: 
      pass 
    if not props["StringFileInfo"]: 
      return (None, None) 
    else: 
      return (props["StringFileInfo"]["CompanName"], props["StringFileInfo"]["ProductName"]) 

希望本文所述对大家的Python程序设计有所帮助。

相关文章

Django数据库类库MySQLdb使用详解

Django项目要操作数据库,首先要和数据库建立连接,才能让程序中的数据和数据库关联起来进行数据的增删改查操作 Django项目默认使用mysqldb模块进行和mysql数据库之间的交互...

python numpy 常用随机数的产生方法的实现

numpy 中 的random模块有多个函数用于生成不同类型的随机数,常见的有 uniform、rand、random、randint、random_interges 下面介绍一下各自的...

详解Python中 sys.argv[]的用法简明解释

详解Python中 sys.argv[]的用法简明解释

因为是看书自学的python,开始后不久就遇到了这个引入的模块函数,且一直在IDLE上编辑了后运行,试图从结果发现它的用途,然而结果一直都是没结果,也在网上查了许多,但发现这个问题的比较...

跟老齐学Python之啰嗦的除法

除法啰嗦的,不仅是python。 整数除以整数 看官请在启动idle之后,练习下面的运算: >>> 2/5 0 >>> 2.0/5 0.4 >...

Python实现的彩票机选器实例

Python实现的彩票机选器实例

本文实例讲述了Python实现彩票机选器的方法。分享给大家供大家参考。具体实现方法如下: # -*- coding: utf8 -*- from Tkinter import * i...