How do I Access File Properties on Windows Vista with Python?

两盒软妹~` 提交于 2019-12-07 13:01:11

问题


The question is as simple as in the title, how do I access windows file properties like date-modifed, and more specifically tags, with Python? For a program I'm doing, I need to get lists of all the tags on various files in a particular folder, and I'm not sure on how to do this. I have the win32 module, but I don't see what I need.

Thanks guys, for the quick responses, however, the main stat I need from the files is the tags attribute now included in windows vista, and unfortunately, that isn't included in the os.stat and stat modules. Thank you though, as I did need this data, too, but it was more of an after-thought on my part.


回答1:


You can use os.stat with stat

import os
import stat
import time

def get_info(file_name):
    time_format = "%m/%d/%Y %I:%M:%S %p"
    file_stats = os.stat(file_name)
    modification_time = time.strftime(time_format,time.localtime(file_stats[stat.ST_MTIME]))
    access_time = time.strftime(time_format,time.localtime(file_stats[stat.ST_ATIME]))
    return modification_time, access_time

You can get lots of other stats, check the stat module for the full list. To extract info for all files in a folder, use os.walk

import os
for root, dirs, files in os.walk(/path/to/your/folder):
    for name in files:
        print get_info(os.path.join(root, name))



回答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:


Example:

import time, datetime
fstat = os.stat(FILENAME)
st_mtime = fstat.st_mtime # Date modified
a,b,c,d,e,f,g,h,i = time.localtime(st_mtime)
print datetime.datetime(a,b,c,d,e,f,g)



回答4:


(You might notice a longer version of the following response given on another of your threads.)

  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, which is iterable. See the following example code and output.


来源:https://stackoverflow.com/questions/1484746/how-do-i-access-file-properties-on-windows-vista-with-python

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