Fork me on GitHub

[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)

        # \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(fname, '\\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(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}}
>>>

  

posted @ 2017-09-23 19:59  [sigai]  阅读(3085)  评论(0编辑  收藏  举报