【问题标题】:Search the last modified ".mxd" files in directory and sub-directory- Python Error在目录和子目录中搜索最后修改的“.mxd”文件 - Python 错误
【发布时间】:2017-02-14 12:07:04
【问题描述】:

我尝试在目录和子目录中查找最后修改的以“.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?,但没有找到任何方法来解决我的问题。

【问题讨论】:

    标签: python-2.7 operating-system directory subdirectory last-modified


    【解决方案1】:

    最后,这段代码运行良好:

    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
    

    【讨论】:

      猜你喜欢
      • 2016-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-29
      • 1970-01-01
      • 2020-10-22
      • 2014-06-13
      • 1970-01-01
      相关资源
      最近更新 更多