如何检索文件';在Windows中使用Python 2的标准库获取详细信息
我需要在Windows中读取文件的详细信息,以便查询文件属性窗口的详细信息选项卡中显示的文件的“文件版本” 我还没有在标准库中找到任何使这一点非常容易实现的东西,但我想如果我能找到正确的windows函数,我可能可以使用ctypes来实现它 是否有人有任何示范代码,或者他们可以告诉我一个Windows函数,让我阅读这些信息。我已经看了一眼,但据我所知,这并不完全正确。使用来自的win32 api。这个api使用起来有点麻烦,但我也需要它,所以我把它作为一个例子放在一起如何检索文件';在Windows中使用Python 2的标准库获取详细信息,python,file,properties,ctypes,details,Python,File,Properties,Ctypes,Details,我需要在Windows中读取文件的详细信息,以便查询文件属性窗口的详细信息选项卡中显示的文件的“文件版本” 我还没有在标准库中找到任何使这一点非常容易实现的东西,但我想如果我能找到正确的windows函数,我可能可以使用ctypes来实现它 是否有人有任何示范代码,或者他们可以告诉我一个Windows函数,让我阅读这些信息。我已经看了一眼,但据我所知,这并不完全正确。使用来自的win32 api。这个api使用起来有点麻烦,但我也需要它,所以我把它作为一个例子放在一起 usage: versi
usage: version_info.py [-h] [--lang LANG] [--codepage CODEPAGE] path
也可以用作模块,请参见VersionInfo类。使用Python2.7和3.6检查了一些文件。这有帮助吗?
import array
from ctypes import *
def get_file_info(filename, info):
"""
Extract information from a file.
"""
# Get size needed for buffer (0 if no info)
size = windll.version.GetFileVersionInfoSizeA(filename, None)
# If no info in file -> empty string
if not size:
return ''
# Create buffer
res = create_string_buffer(size)
# Load file informations into buffer res
windll.version.GetFileVersionInfoA(filename, None, size, res)
r = c_uint()
l = c_uint()
# Look for codepages
windll.version.VerQueryValueA(res, '\\VarFileInfo\\Translation',
byref(r), byref(l))
# If no codepage -> empty string
if not l.value:
return ''
# Take the first codepage (what else ?)
codepages = array.array('H', string_at(r.value, l.value))
codepage = tuple(codepages[:2].tolist())
# Extract information
windll.version.VerQueryValueA(res, ('\\StringFileInfo\\%04x%04x\\'
+ info) % codepage, byref(r), byref(l))
return string_at(r.value, l.value)