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

yipeiwu_com6年前Python基础

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

区别于前文所述。本例是在linux下得到文件版本信息,主要是通过pefile模块解析文件 中的字符串得到的。代码如下:

  def _get_company_and_product(self, file_path): 
    """ 
    Read all properties of the given file return them as a dictionary. 
    @return: a tumple, (company, product) 
    """ 
    mype = pefile.PE(file_path) 
    companyName = "" 
    productName = "" 
      
    if hasattr(mype, 'VS_VERSIONINFO'): 
      if hasattr(mype, 'FileInfo'): 
        for entry in mype.FileInfo: 
          if hasattr(entry, 'StringTable'): 
            for st in entry.StringTable: 
              for k, v in st.entries.items(): 
                if k == u"CompanyName": 
                  companyName = v 
                elif k == u"ProductName": 
                  productName = v 
    if not companyName: 
      companyName = None 
    if not productName: 
      productName = None 
    return (companyName, productName) 

这里我们只要了公司名称信息和产品名称信息。至于版本号之类的信息也是在字符串资源中。

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

相关文章

python matplotlib画图库学习绘制常用的图

python matplotlib画图库学习绘制常用的图

本文实例为大家分享了python matplotlib绘制常用图的具体代码,供大家参考,具体内容如下 github地址 导入相关类 import numpy as np import...

python实现备份目录的方法

本文实例讲述了python实现备份目录的方法。分享给大家供大家参考。具体如下: 备份脚本1: #!/usr/bin/python # Filename: backup_ver1.py...

Python的Flask框架标配模板引擎Jinja2的使用教程

Jinja2需要Python2.4以上的版本。 安装 按照Jinja有多种方式,你可以根据需要选择不同的按照方式。 使用easy_install 或pip: #sudo e...

python机器学习理论与实战(二)决策树

python机器学习理论与实战(二)决策树

        决策树也是有监督机器学习方法。 电影《无耻混蛋》里有一幕游戏,在德军小酒馆里有几个人在玩20问题游戏...

Python使用turtle库绘制小猪佩奇(实例代码)

Python使用turtle库绘制小猪佩奇(实例代码)

turtle(海龟)是Python重要的标准库之一,它能够进行基本的图形绘制。turtle图形绘制的概念诞生于1969年,成功应用于LOGO编程语言。 turtle库绘制图形有一个基本...