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

社会主义新天地 提交于 2019-12-05 21:48:29

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))

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!

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)
JellicleCat

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