英文:
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's [os library][1] with Windows' [Shell.Application][2] object, as done [here][3], something like this:
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): # list all files
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)
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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论