Python method to get file version information company name and product name under Linux

  • 2020-04-02 14:11:53
  • OfStack

This article is an example of how python can get Linux file version information, company name, and product name. The details are as follows:

It is different from the above mentioned. This example is to get the file version information under Linux, mainly through the pefile module to parse the string in the file. The code is as follows:


  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) 

Here we just need the company name information and the product name information. Information such as the version number is also in the string resource.

I hope this article has helped you with your Python programming.


Related articles: