如何使用Python按名称搜索文件、查找最新修改的文件、查找下一个文件并重复步骤?

2024-04-27 22:27:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个文件夹,其中有许多DWG文件,一些文件的例子可能是

LINCOLN 09-17 #1-29H ASBUILT 10-26-2010.dwg
LINCOLN 09-17 #1-29H FINAL 01-20-2011.dwg
CAMPBELL 07-17 #5-29H DRAFT 01-20-2011.dwg
CAMPBELL 07-17 #5-29H FINAL 01-27-2011.dwg
CAMPBELL 07-17 #5-29H FINAL 01-27-2011_1.dwg

我需要捕捉(也许在一个列表中?)只有具有FINAL的dwg文件是该文件名的最新修改的dwg文件。所以上面要捕获的文件是

^{pr2}$

有什么建议吗?在

主持人,我似乎不能为下面的问题添加评论。为什么呢??在

这是我到目前为止的情况。。我在我认为需要修改的地方发表了评论

import os 

rootdir='c:\Your\Path'

for subdir, dirs, files in os.walk(rootdir):    
    for file in files:

    # I need to check only files with the same name so I can 
    # get the latest of those files before checking next files      

        if os.path.getmtime(file):
            # I believe this will get time file was modified but
            # but how do I store this to compare to other files
            # with this name??

Tags: 文件toinforoswith评论files
1条回答
网友
1楼 · 发布于 2024-04-27 22:27:08

你需要去寻找合适的文件,并记住每个文件中最新的 你过来看看。然后报告最新情况。在

在伪代码中。在

dates = dict() // key is filename, value is date
for  subdir,dirs, files in os.walk(startdir):
if filename ends with "dwg":
     if filename contains "FINAL":
          date = getDate from filename 
          if dates contains filename
              prev = dates(filename)
          else 
              prev = zero date // a value so next line tests true always
          if date > prev:
             dates(filename) = date
          // else previous is younger, so skip
    // else draft or as built and skip
// else filename not dwg and skip
for dates.values as v
    print v

相关问题 更多 >