【问题标题】:How can I get information on a file's Properties>Details using python?如何使用 python 获取有关文件属性>详细信息的信息?
【发布时间】:2018-07-14 01:06:50
【问题描述】:

到目前为止,我只成功获得了使用此代码的版本。

from win32com.client import Dispatch    
ver_parser = Dispatch('Scripting.FileSystemObject')
info = ver_parser.GetFileVersion(path + "\\" + file)

现在,我只知道“GetFileVersion”,我的 IDE 不会自动完成以显示任何其他选项来补充其他信息

示例文件属性>详细信息:

【问题讨论】:

标签: python win32com


【解决方案1】:

我认为您可以使用“Shell.Application”来获取文件元信息。如下所示替换文件夹名称和文件名。

from win32com.client import Dispatch
shell = Dispatch("Shell.Application")
_dict = {}
# enter directory where your file is located
ns = shell.NameSpace("D:\\Userfiles\\Downloads")
for i in ns.Items():
    # Check here with the specific filename
    if str(i) == "Test.png":
        for j in range(0,49):
            _dict[ns.GetDetailsOf(j,j)] = ns.GetDetailsOf(i,j)

print _dict

【讨论】:

  • 我已经测试过这种方法并且它有效,但是它很慢。
【解决方案2】:

根据this site,您需要一个COM 浏览器来查看哪些方法和属性可用。

对于一个非常基本的,做:

from win32com.client import combrowse
combrowse.main()

【讨论】:

    【解决方案3】:

    您可以使用win32com.client.gencache.EnsureDispatch 生成对应COM模块的Python代码。有了这个,您可以使用 __dir__ 访问所有方法的名称(您的 IDE 很可能使用 __dir__ 自动完成代码,以便您也能得到它);

    In [204]: from win32com.client.gencache import EnsureDispatch
    
    In [205]: ver_parser = EnsureDispatch('Scripting.FileSystemObject')
    
    In [210]: [a for a in ver_parser.__dir__() if '_' not in a]
    Out[210]:
    ['CLSID',
     'BuildPath',
     'CopyFile',
     'CopyFolder',
     'CreateFolder',
     'CreateTextFile',
     'DeleteFile',
     'DeleteFolder',
     'DriveExists',
     'FileExists',
     'FolderExists',
     'GetAbsolutePathName',
     'GetBaseName',
     'GetDrive',
     'GetDriveName',
     'GetExtensionName',
     'GetFile',
     'GetFileName',
     'GetFileVersion',
     'GetFolder',
     'GetParentFolderName',
     'GetSpecialFolder',
     'GetStandardStream',
     'GetTempName',
     'MoveFile',
     'MoveFolder',
     'OpenTextFile']
    

    请注意,这只会让您获得方法的名称,而不是属性的名称 (yet),但您可以手动获得:

    In [213]: set(ver_parser._prop_map_get_).union(set(ver_parser._prop_map_put_))
    Out[213]: {'Drives'}
    

    相关堆栈溢出问题:

    【讨论】:

      猜你喜欢
      • 2014-10-15
      • 1970-01-01
      • 1970-01-01
      • 2011-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-14
      相关资源
      最近更新 更多