views:

545

answers:

3

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.

+2  A: 

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))
Nadia Alramli
+1  A: 

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)
+1  A: 

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!

Alex Martelli