如何使用 Python 在 Windows Vista 上访问文件属性? [英] How do I Access File Properties on Windows Vista with Python?

查看:38
本文介绍了如何使用 Python 在 Windows Vista 上访问文件属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题和标题一样简单,我如何使用 Python 访问 windows 文件属性,如日期修改,更具体地说是标签?对于我正在执行的程序,我需要获取特定文件夹中各种文件上所有标签的列表,但我不确定如何执行此操作.我有 win32 模块,但我看不到我需要什么.

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.

谢谢大家的快速回复,但是,我需要从文件中获取的主要统计信息是现在包含在 windows vista 中的标签属性,不幸的是,它没有包含在 os.stat 和 stat 模块中.谢谢你,因为我也需要这些数据,但这更多是我事后的想法.

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.

推荐答案

您可以使用 os.statstat

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

您可以获得许多其他统计信息,请查看统计模块以获取完整列表.要提取文件夹中所有文件的信息,请使用 os.walk

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

这篇关于如何使用 Python 在 Windows Vista 上访问文件属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆