Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何检索文件';在Windows中使用Python 2的标准库获取详细信息_Python_File_Properties_Ctypes_Details - Fatal编程技术网

如何检索文件';在Windows中使用Python 2的标准库获取详细信息

如何检索文件';在Windows中使用Python 2的标准库获取详细信息,python,file,properties,ctypes,details,Python,File,Properties,Ctypes,Details,我需要在Windows中读取文件的详细信息,以便查询文件属性窗口的详细信息选项卡中显示的文件的“文件版本” 我还没有在标准库中找到任何使这一点非常容易实现的东西,但我想如果我能找到正确的windows函数,我可能可以使用ctypes来实现它 是否有人有任何示范代码,或者他们可以告诉我一个Windows函数,让我阅读这些信息。我已经看了一眼,但据我所知,这并不完全正确。使用来自的win32 api。这个api使用起来有点麻烦,但我也需要它,所以我把它作为一个例子放在一起 usage: versi

我需要在Windows中读取文件的详细信息,以便查询文件属性窗口的详细信息选项卡中显示的文件的“文件版本”

我还没有在标准库中找到任何使这一点非常容易实现的东西,但我想如果我能找到正确的windows函数,我可能可以使用ctypes来实现它

是否有人有任何示范代码,或者他们可以告诉我一个Windows函数,让我阅读这些信息。我已经看了一眼,但据我所知,这并不完全正确。

使用来自的win32 api。这个api使用起来有点麻烦,但我也需要它,所以我把它作为一个例子放在一起

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)