在目录和子目录中搜索最后修改的“.mxd”文件 - Python 错误

Search the last modified ".mxd" files in directory and sub-directory- Python Error

我尝试在目录和子目录中找到最后修改的以“.mxd”结尾的文件并打印修改时间,使用此代码:

import os

max_mtime = 0
for dirname,subdirs,files in os.walk(r"G:\desktop\Project"):
    for fname in files:
        if fname.endswith(".mxd"):
            full_path = os.path.join(dirname, fname)
            mtime = os.stat(full_path).st_mtime
            if mtime > max_mtime:
                max_mtime = mtime
                max_dir = dirname
                max_file = fname
                print os.path.getatime(fname)

print max_dir, max_file

但是当我运行这段代码时它引发了一个错误,我不明白我的错误是什么:

WindowsError: [Error 2] : 'project.mxd'

我红了 How to get file creation & modification date/times in Python? 但没有找到解决问题的方法。

最后,这段代码效果很好:

import os,time,datetime,glob

path = r"G:\desktop\Project"
for dirname,subdirs,files in os.walk(path):
    max_mtime = 0
    max_dir = ""
    max_file =""
    for fname in files:
        mtime=0
        if fname.endswith(".mxd"):
            full_path = os.path.join(dirname, fname)
            mtime = os.stat(full_path).st_mtime
            if mtime > max_mtime:
                max_mtime = mtime
                max_dir = dirname
                max_file = fname
    print max_dir, max_file
    print