zoukankan      html  css  js  c++  java
  • [Python]获取win平台文件的详细信息

    import win32api
    
    def getFileProperties(fname):
        """
        读取给定文件的所有属性, 返回一个字典.
        """
        propNames = ('Comments', 'InternalName', 'ProductName',
            'CompanyName', 'LegalCopyright', 'ProductVersion',
            'FileDescription', 'LegalTrademarks', 'PrivateBuild',
            'FileVersion', 'OriginalFilename', 'SpecialBuild')
    
        props = {'FixedFileInfo': None, 'StringFileInfo': None, 'FileVersion': None}
    
        try:
            fixedInfo = win32api.GetFileVersionInfo(fname, '\')
            props['FixedFileInfo'] = fixedInfo
            props['FileVersion'] = "%d.%d.%d.%d" % (fixedInfo['FileVersionMS'] / 65536,
                    fixedInfo['FileVersionMS'] % 65536, fixedInfo['FileVersionLS'] / 65536,
                    fixedInfo['FileVersionLS'] % 65536)
    
            # VarFileInfoTranslation 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(fname, '\VarFileInfo\Translation')[0]
    
            # any other must be of the form StringfileInfo\%04X%04Xparm_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(fname, strInfoPath)
    
            props['StringFileInfo'] = strInfo
        except:
            pass
    
        return props
    
    
    if __name__ = "__main__":
      getFileProperties(./python.exe)
    

      

    https://stackoverflow.com/a/7993095/2615376

    查看python.exe返回:

    >>> pprint(getFileProperties(path))
    {'FileVersion': '3.6.2150.1013',
     'FixedFileInfo': {'FileDate': None,
                       'FileFlags': 0,
                       'FileFlagsMask': 63,
                       'FileOS': 4,
                       'FileSubtype': 0,
                       'FileType': 1,
                       'FileVersionLS': 140903413,
                       'FileVersionMS': 196614,
                       'ProductVersionLS': 140903413,
                       'ProductVersionMS': 196614,
                       'Signature': -17890115,
                       'StrucVersion': 65536},
     'StringFileInfo': {'Comments': None,
                        'CompanyName': 'Python Software Foundation',
                        'FileDescription': 'Python',
                        'FileVersion': '3.6.2',
                        'InternalName': 'Python Console',
                        'LegalCopyright': 'Copyright © 2001-2016 Python Software '
                                          'Foundation. Copyright © 2000 '
                                          'BeOpen.com. Copyright © 1995-2001 CNRI. '
    
                                          'Copyright © 1991-1995 SMC.',
                        'LegalTrademarks': None,
                        'OriginalFilename': 'python.exe',
                        'PrivateBuild': None,
                        'ProductName': 'Python',
                        'ProductVersion': '3.6.2',
                        'SpecialBuild': None}}
    >>>

      

  • 相关阅读:
    完美解决IE8有两个进程的问题
    用ccproxy + stunnel做个加密代理
    Hyper-V 共享式网络链接 端口映射
    NET Framework 4.0的安装失败处理
    c#控制IE浏览器自动点击等事件WebBrowser,mshtml.IHTMLDocument2 .
    设置IE8 多个Table只产生一个进程
    SSH Secure Shell Client中文乱码的解决办法
    OOD设计模式
    MVC设计模式
    乐观锁和悲观锁
  • 原文地址:https://www.cnblogs.com/sigai/p/7582367.html
Copyright © 2011-2022 走看看