python列出指定目录"c:\"所有的后缀名为*.txt 的文件(包括子文件夹内所有文件)

并输出每个文件的创建日期和大小

import os
import fnmatch
import time

def findfiles(path, fnmatchex='*.*):
    for root, dirs, files in os.walk(path):
        for filename in fnmatch.filter(fnmatchex, files):
            fullname = os.path.join(root, filename)
            filestat = os.stat(fullname)
            yield fullname, filestat.st_size, filestat.st_ctime

def strtimestamp(timestamp):
    return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(timestamp))

for filename, filesize, createtime in findfiles(r"C:\", "*.txt"):
    print filename, filesize, strtimestamp(createtime)

追问

那个 我用的是window7的Python3.3.5版本

追答import os
import fnmatch
import time
 
def findfiles(path, fnmatchex='*.*'):
    for root, dirs, files in os.walk(path):
        for filename in fnmatch.filter(fnmatchex, files):
            fullname = os.path.join(root, filename)
            filestat = os.stat(fullname)
            yield fullname, filestat.st_size, filestat.st_ctime
 
def strtimestamp(timestamp):
    return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(timestamp))
 
for filename, filesize, createtime in findfiles(r"C:\", "*.txt"):
    print filename, filesize, strtimestamp(createtime)

温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-11-10
相似回答