问题
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.)
- Download and install the pywin32 extension.
- Grab the code Tim Golden wrote for this very task.
- Save Tim's code as a module on your own computer.
- 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