2017-04-26 4 views
2

ファイルの詳細タブに表示されるファイルの 'ファイルバージョン'を調べるために、Windowsのファイルの詳細を読む必要がありますプロパティウィンドウ。Python 2の標準ライブラリを使用してWindowsでファイルの詳細を取得する方法

File Properties Dialog

私は達成するために、この非常に簡単になり、標準ライブラリには何も見つかりましたが、私は右の窓関数を見つけることができれば、私はおそらくctypesのを使用して、それを達成することができ考え出していません。

誰も模範的なコードを持っているのですか、私はこの情報を読むことができるWindowsの機能を教えてくれますか?私はすでにGetFileAttributesを見ましたが、それは私が知る限り、それほど正しいものではありませんでした。

+0

これは次のようになります:http://stackoverflow.com/questions/12521525/reading-metadata-with-python help you? – 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) 
関連する問題