好的,所以我提出的解决方案使用了@hughdbrown与操作系统统计(). 我用过操作系统统计()获取上次修改文件时的st_mtime属性,该属性可用于显示文件是否已从一个时间点覆盖到另一个时间点。我把所有的东西都存储在字典中,文件名在第一时间作为键作为值。在import os
workdir = '/path/to/directory'
preFileStats = {}
for filename in os.listdir(workdir):
preFileStats[filename] = os.stat(workdir + "/" + filename).st_mtime
subprocess.(processArguments)
postFileStats = {}
for filename in os.listdir(workdir):
postFileStats[filename] = os.stat(workdir + "/" + filename).st_mtime
class DictDiffer(object):
"""
Calculate the difference between two dictionaries as:
(1) items added
(2) items removed
(3) keys same in both but changed values
(4) keys same in both and unchanged values
"""
def __init__(self, current_dict, past_dict):
self.current_dict, self.past_dict = current_dict, past_dict
self.set_current, self.set_past = set(current_dict.keys()), set(past_dict.keys())
self.intersect = self.set_current.intersection(self.set_past)
def added(self):
return self.set_current - self.intersect
def removed(self):
return self.set_past - self.intersect
def changed(self):
return set(o for o in self.intersect if self.past_dict[o] != self.current_dict[o])
def unchanged(self):
return set(o for o in self.intersect if self.past_dict[o] == self.current_dict[o])
d = DictDiffer(postFileStats, preFileStats)
newFiles = list(d.changed()) + list(d.added())
当然,dictdifference类非常强大,可以用来检查删除的文件或未更改的文件。在