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

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

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]所示的一样,类似这样:

  1. import os
  2. import win32com.client
  3. sh = win32com.client.gencache.EnsureDispatch('Shell.Application', 0)
  4. path = r'c:\mypath\myfolder'
  5. ns = sh.NameSpace(path)
  6. colnum = 0
  7. columns = []
  8. while True:
  9. colname = ns.GetDetailsOf(None, colnum)
  10. if not colname:
  11. break
  12. columns.append(colname)
  13. colnum += 1
  14. for name in os.listdir(path): # 列出所有文件
  15. print(path + '\\' + name)
  16. item = ns.ParseName(name)
  17. for colnum in range(len(columns)):
  18. colval = ns.GetDetailsOf(item, colnum)
  19. if colval:
  20. print('\t', columns[colnum], colval)
  21. 隐藏文件将显示H属性表示隐藏

...
Attributes HA
...

  1. [1]: https://docs.python.org/3/library/os.html
  2. [2]: https://learn.microsoft.com/en-us/windows/win32/shell/shell-application
  3. [3]: https://stackoverflow.com/a/12541871/403671
  4. <details>
  5. <summary>英文:</summary>
  6. You can combine python&#39;s [os library][1] with Windows&#39; [Shell.Application][2] object, as done [here][3], something like this:
  7. import os
  8. import win32com.client
  9. sh = win32com.client.gencache.EnsureDispatch(&#39;Shell.Application&#39;, 0)
  10. path = r&#39;c:\mypath\myfolder&#39;
  11. ns = sh.NameSpace(path)
  12. colnum = 0
  13. columns = []
  14. while True:
  15. colname=ns.GetDetailsOf(None, colnum)
  16. if not colname:
  17. break
  18. columns.append(colname)
  19. colnum += 1
  20. for name in os.listdir(path): # list all files
  21. print(path + &#39;\\&#39; + name)
  22. item = ns.ParseName(name)
  23. for colnum in range(len(columns)):
  24. colval=ns.GetDetailsOf(item, colnum)
  25. if colval:
  26. print(&#39;\t&#39;, columns[colnum], colval)
  27. hidden files will display (H attribute is for Hidden)
  28. ...
  29. Attributes HA
  30. ...
  31. [1]: https://docs.python.org/3/library/os.html
  32. [2]: https://learn.microsoft.com/en-us/windows/win32/shell/shell-application
  33. [3]: https://stackoverflow.com/a/12541871/403671
  34. </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:

确定