2017-04-26 13 views
2

파일의 세부 정보 탭에 표시된대로 파일의 '파일 버전'을 조사 할 수 있도록 Windows의 파일 세부 정보를 읽어야합니다. 속성 창.Python 2 용 표준 라이브러리를 사용하여 Windows에서 파일의 세부 정보를 검색하는 방법

File Properties Dialog

나는 달성이 매우 쉽게 표준 라이브러리에서 아무것도 발견하지만 오른쪽 창 기능을 찾을 수 있다면, 아마하는 ctypes를 사용하여 수행 할 수있는 생각하지 않았습니다.

누구나 모범적 인 코드가 있거나이 정보를 읽을 수있는 Windows 기능을 가르쳐 줄 수 있습니까? 나는 이미 GetFileAttributes을 보았지만, 내가 말할 수있는 한 그것은 옳지 않았다.

+0

이렇게 하시겠습니까? http://stackoverflow.com/questions/12521525/reading-metadata-with-python 도움이 되셨습니까? – thebjorn

답변

0

ctypes에서 win32 api Version Information functions을 사용하십시오. API는 사용하기에 약간의 실수이지만, 예를 들어 a quick script을 함께 던져 넣기를 바랍니다.

usage: version_info.py [-h] [--lang LANG] [--codepage CODEPAGE] path 

모듈로 사용할 수도 있습니다. 자세한 내용은 VersionInfo 클래스를 참조하십시오. Python 2.7 및 3.6에서 몇 개의 파일에 대해 확인했습니다.

+0

알립니다. 나를 올바른 방향으로 가리켜 주셔서 감사합니다. – MrBubbles

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