How do you retrieve the tags of a file in a list with Python (Windows Vista)?

血红的双手。 提交于 2019-11-26 11:27:57

问题


I want to make something of a tag cloud for various folders I have, but unfortunately, I can\'t seem to find a way to access the tags of a file in Windows Vista. I tried looking at the win32 module, and os.stat, but I can\'t seem to find a way. Can I get some help on this?


回答1:


I went about it with the win32 extensions package, along with some demo code I found. I posted a detailed explanation of the process on this thread. I don't want to reproduce it all here, but here is the short version (click the foregoing link for the details).

  1. Download and install the pywin32 extension.
  2. Grab the code Tim Golden wrote for this very task.
  3. Save Tim's code as a module on your own computer.
  4. Call the property_sets method of your new module (supplying the necessary filepath). The method returns a generator object, which is iterable. See the following example code and output.

(This works for me in XP, at least.)

E.g.

import your_new_module
propgenerator= your_new_module.property_sets('[your file path]')
    for name, properties in propgenerator:
        print name
        for k, v in properties.items ():
            print "  ", k, "=>", v

The output of the above code will be something like the following:

DocSummaryInformation
   PIDDSI_CATEGORY => qux
SummaryInformation
   PIDSI_TITLE => foo
   PIDSI_COMMENTS => flam
   PIDSI_AUTHOR => baz
   PIDSI_KEYWORDS => flim
   PIDSI_SUBJECT => bar



回答2:


Apparently, you need to use the Windows Search API looking for System.Keywords -- you can access the API directly via ctypes, or indirectly (needing win32 extensions) through the API's COM Interop assembly. Sorry, I have no vista installation on which to check, but I hope these links are useful!




回答3:


It appears that Windows stores the tags in the files. Just tag any image and open the image in notepad and look for something XML-like(RDF) and you will find your tag there. Well... now we know that they are indeed stored in the files, but we still have no idea how to manipulate them.

But google is to the rescue. I googled: windows metadata api

and found this: http://blogs.msdn.com/pix/archive/2006/12/06/photo-metadata-apis.aspx




回答4:


There are actually 2 different implentations of document properties (source).

  1. The COM implementation embeds them directly in the file itself : this is the approach used for Office documents for example. Tim Golden's code described on this page works well for these.

  2. On NTFS 5 (Win2k or later), you can add Summary info to any file, and it's stored in alternate data streams. I suppose the Windows Search API would work on these, but I have not tested it.



来源:https://stackoverflow.com/questions/1512435/how-do-you-retrieve-the-tags-of-a-file-in-a-list-with-python-windows-vista

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!