监视文件以进行修改

时间:2018-05-19 08:02:50

标签: julia

我正在尝试编写一个简单的代码,该代码会不断检查给定文件夹中的文件何时被修改,并在修改后触发一个函数。

我已经在下面添加了一个MWE,它的工作原理是"但我想知道是否

  • 我应该查看哪些库或实用程序可以帮助解决这个问题?
  • 有一种更清洁的方法可以持续运行,直到某个事件(这里是中断)
files_and_times = Dict{String, Int}()
for (root, _, files) ∈ walkdir(dirpath)
    for f ∈ files
        fpath = joinpath(root, f)
        files_and_times[fpath] = stat(fpath).mtime
    end
end
try
    while true
        for (f, t) ∈ files_and_times
            cur_t = stat(f).mtime
            if cur_t > t
                files_and_times[f] = cur_t
                println("file $f was modified")
            end
        end
        sleep(0.5)
    end
catch x
    if isa(x, InterruptException)
        println("Shutting down.")
    else
        throw(x)
    end
end

1 个答案:

答案 0 :(得分:3)

@ rickhg12hs和@Colin_T_Bowers的评论有效地回答了我的问题。发布答案以结束问题。

相关问题