我有一个包含许多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文件。所以上面的文件将被捕获
LINCOLN 09-17 #1-29 FINAL 01-20-2011.dwg
CAMPBELL 07-17 #5-29H FINAL 01-27-2011_1.dwg
有什么建议吗?
主持人,我似乎无法在下面的问题中添加评论。为什么???
这是我到目前为止所做的......我把评论放在我认为需要改变的地方
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??
答案 0 :(得分:0)
您需要搜索合适的文件,并记住每个文件的最新文件 你遇到过它。然后报告最新的。
在伪代码中。
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