Python获取Windows上隐藏文件的元数据

huangapple go评论62阅读模式
英文:

Python get meta data for hidden files on windows

问题

我一直在使用这里的回复来读取 Windows 上文件的元数据。然而,我注意到它会忽略隐藏文件。

如何在这种方法中包括隐藏文件?

英文:

I have been using the replies from here to read out the metadata of files on windows.
However i noticed that it would just ignore hidden files.

How can one also include hidden files in this approach?

答案1

得分: 1

你可以结合Python的os库和Windows的[Shell.Application][2]对象,就像[这里][3]所示的一样,类似这样:

import os
import win32com.client

sh = win32com.client.gencache.EnsureDispatch('Shell.Application', 0)
path = r'c:\mypath\myfolder'
ns = sh.NameSpace(path)

colnum = 0
columns = []
while True:
    colname = ns.GetDetailsOf(None, colnum)
    if not colname:
        break
    columns.append(colname)
    colnum += 1

for name in os.listdir(path): # 列出所有文件
    print(path + '\\' + name)
    item = ns.ParseName(name)
    for colnum in range(len(columns)):
        colval = ns.GetDetailsOf(item, colnum)
        if colval:
            print('\t', columns[colnum], colval)

隐藏文件将显示H属性表示隐藏

...
Attributes HA
...


[1]: https://docs.python.org/3/library/os.html
[2]: https://learn.microsoft.com/en-us/windows/win32/shell/shell-application
[3]: https://stackoverflow.com/a/12541871/403671

<details>
<summary>英文:</summary>

You can combine python&#39;s [os library][1] with Windows&#39; [Shell.Application][2] object, as done [here][3], something like this:

    import os
    import win32com.client
    
    sh = win32com.client.gencache.EnsureDispatch(&#39;Shell.Application&#39;, 0)
    path = r&#39;c:\mypath\myfolder&#39;
    ns = sh.NameSpace(path)
    
    colnum = 0
    columns = []
    while True:
        colname=ns.GetDetailsOf(None, colnum)
        if not colname:
            break
        columns.append(colname)
        colnum += 1
    
    for name in os.listdir(path): # list all files
        print(path + &#39;\\&#39; + name)
        item = ns.ParseName(name)
        for colnum in range(len(columns)):
            colval=ns.GetDetailsOf(item, colnum)
            if colval:
                print(&#39;\t&#39;, columns[colnum], colval)

hidden files will display (H attribute is for Hidden)

    ...
    Attributes HA
    ...

  [1]: https://docs.python.org/3/library/os.html
  [2]: https://learn.microsoft.com/en-us/windows/win32/shell/shell-application
  [3]: https://stackoverflow.com/a/12541871/403671

</details>



huangapple
  • 本文由 发表于 2023年2月6日 03:21:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75354898.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定