Python,检查文件保存的时间

时间:2014-07-13 22:35:36

标签: python file-io file-management

目标是每次修改(保存)列表中的文件时执行某些操作。

我不知道要继续。最终目标应该是这样的:

files = ['file1.txt', 'file2.txt', 'file3,txt']

if (one of the files in files is modified):
    Print '%s has been modified' % (filename)

1 个答案:

答案 0 :(得分:1)

这将使用dict检查时间并打印任何已修改的文件:

 import os.path, time
files = ['file1.txt', 'file2.txt', 'file3,txt']
changes =  {"file1.txt":os.path.getmtime("file1.txt"),"file2.txt":os.path.getmtime("file2.txt"),"file3.txt":os.path.getmtime("file3.txt")}
while True:
    for f in files:
        if changes.get(f) < os.path.getmtime(f):
            print "File {} has been modified".format(f)
            changes[f] = os.path.getmtime(f)
        else:
            print "No changes, going to sleep."
    time.sleep(10)