gpt4 book ai didi

python - 如何在 Windows 上访问文件的属性?

转载 作者:IT老高 更新时间:2023-10-28 22:23:30 25 4
gpt4 key购买 nike

上次我问了一个类似的问题,但那是关于 svn 相关的版本信息。现在我想知道如何查询窗口“文件版本”属性,例如。一个dll。我也关注了 wmi 和 win32file 模块,但没有成功。

最佳答案

这是一个将所有文件属性作为字典读取的函数:

import win32api

#==============================================================================
def getFileProperties(fname):
#==============================================================================
"""
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(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

关于python - 如何在 Windows 上访问文件的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/580924/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com